showsUserLocation returns pin instead of blue dot in iPhone simulator

喜你入骨 提交于 2019-12-07 09:56:53

问题


This is my -mapView:viewForAnnotation method which drops pins when i create annotation views. But when i set mapView.showsUserLocation = YES; in -viewDidLoad, i get a pin dropped on Infinite Loop (expected - in simulator) and not the normal blue dot.

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
     MKAnnotationView *anno = nil;
     //create a pin annotation view
MKPinAnnotationView  *pin=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"]autorelease];

    [pin setPinColor:MKPinAnnotationColorRed];
    pin.animatesDrop=YES;
    pin.canShowCallout = YES;
    pin.calloutOffset = CGPointMake(-5, 5);
    anno = pin;
    return anno;
}

How can i get it to drop pins and show the blue dot as well?

Thanks


回答1:


Really simple to fix, although unsure if this is the correct way to do it...

if (annotation == mapView.userLocation){
    return nil; //default to blue dot
}



回答2:


Similar to the other answer, here's something close:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
    NSString *annotationType = [NSString stringWithCString:object_getClassName(annotation)];
    if ([annotationType compare:@"NSKVONotifying_MKUserLocation"] == 0)
        return nil;
    ...
}

Of course, use something like this at your own risk. It could stop working tomorrow if Apple decided to change that name.




回答3:


Often you use your own class of annotation to look up information related to the annotation. In that case, to only handle your own annotations, use something like

if ([annotation isKindOfClass:[MapLocation class]]) {}


来源:https://stackoverflow.com/questions/1853144/showsuserlocation-returns-pin-instead-of-blue-dot-in-iphone-simulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!