使用MKMapView在iOS地图上的用户当前位置上自定义Pin

时间:2016-02-04 17:09:44

标签: ios objective-c gps mkmapview

我正在尝试在用户的当前位置放置一个引脚。从LocationManger挑选位置并将其分配到MKAnnotation。但是当我运行应用程序时,Map会在地图上的不同位置显示用户位置和Pin位置。不是很远,但我想知道为什么它不一样。其中自定义引脚和用户位置在模拟器上相同。

以下是一些示例代码。

self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];

MyLocation *mylocation = [[MyLocation alloc] initWithName:@"name" address:@"address" coordinate:self.locationManager.location.coordinate] ;

我需要设置一些准确度吗?想知道MKMapView如何显示位置。

1 个答案:

答案 0 :(得分:0)

试试这段代码:

#pragma mark -
#pragma mark MKMapView delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(annotationView)
        return annotationView;
    else
    {
        MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                         reuseIdentifier:AnnotationIdentifier] autorelease];
        annotationView.canShowCallout = YES;
        annotationView.image = [UIImage imageNamed:@"someImage.png"];
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        annotationView.rightCalloutAccessoryView = rightButton;
        annotationView.canShowCallout = YES;
        annotationView.draggable = YES;
        return annotationView;
    }
    return nil;
 }

更多细节更喜欢this