简体   繁体   English

iOS:获取职位用户的最佳方法

[英]iOS: Best way to get position user

I have a MKMapView class, and I want to show on the map the user position represented by a image. 我有一个MKMapView类,并且我想在地图上显示由图像表示的用户位置。 So, I read this tutorial: http://www.switchonthecode.com/tutorials/getting-your-location-in-an-iphone-application 因此,我阅读了本教程: http : //www.switchonthecode.com/tutorials/getting-your-location-in-an-iphone-application

This guy use the locationmanager on the view, I need to use it on a MKMapView. 这个家伙在视图上使用locationmanager,我需要在MKMapView上使用它。

For me, the best way to do this is creating a class just for control the user position? 对我来说,最好的方法是创建一个仅用于控制用户位置的类? How can I do this? 我怎样才能做到这一点?

Thanks!! 谢谢!!

First you want the location to show up on your map: 首先,您希望该位置显示在地图上:

yourMapView.showsUserLocation = YES;

Then you need to handle the delegate method for displaying an annotationview for the user location: 然后,您需要处理用于显示用户位置注释视图的委托方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        // Create a custom MKAnnotationView here that will display your image.
    }
}

Make sure you set the delegate on your MKMapView properly. 确保在MKMapView正确设置了delegate

For this you need to implement the MKAnnotation & MKMapView Delegates. 为此,您需要实现MKAnnotationMKMapView委托。

Implement this method for changing the default pin to the custom image. 实现此方法以将默认引脚更改为自定义图像。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
  {
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        // Create a custom MKAnnotationView here that will display your image.
        //user current location
    }

    static NSString *identifier = @"MyLocation";   
    if ([annotation isKindOfClass:[MyLocation class]])
    {

        MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil)
        {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        }
        else
        {
            annotationView.annotation = annotation;
        }

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.image=[UIImage imageNamed:@"yourImage.png"];

        return annotationView;
    }

    return nil;    
}

Please refer this tutorial . 请参考本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM