Domanda

I have pins that drop on a map and the current location is shown with a blue dot. I added:

- (MKAnnotationView *)mapView:(MKMapView *)amapView viewForAnnotation:(id<MKAnnotation>)annotation{


    NSString *identifier =@"mypin";
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [amapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if(pin ==nil){
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
    }else{
        pin.annotation = annotation;
    }

    UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    myDetailButton.frame = CGRectMake(0, 0, 23, 23);
    myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [myDetailButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

    pin.rightCalloutAccessoryView = myDetailButton;
    pin.enabled = YES;
    pin.animatesDrop = TRUE;
    pin.canShowCallout = YES;

    return pin;
}

But now the current location is a pin instead of the blue dot. How can I stop the annotation for the current location from becoming a pin and keep it as the blue dot.

Any help please?

È stato utile?

Soluzione

The "blue dot" is a special annotation, a MKUserLocation. So, in your viewForAnnotation, just add the following two lines at the start to tell iOS to use the standard "blue dot" for the user location annotation:

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

By returning nil, you will tell iOS to use the default "blue dot" annotation for the user location.

For an example of this in practice, see the code sample in Creating Annotation Views from Your Delegate Object section of the Location Awareness Programming Guide.

Altri suggerimenti

Simple :

- (MKAnnotationView *)mapView :(MKMapView *)maapView viewForAnnotation:(id <MKAnnotation>) annotation{
    @autoreleasepool {


    if (annotation == maapView.userLocation)
    {
        // This code will execute when the current location is called.
        return nil;
    }
    else
    {
        MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
        annView.pinColor = MKPinAnnotationColorPurple;
        annView.animatesDrop=YES;
        annView.canShowCallout = YES;
        annView.calloutOffset = CGPointMake(-5, 5);
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];

        annView.rightCalloutAccessoryView = rightButton;
        return annView;
    }

}
}

Clean and Swifty (3) way

if annotation is MKUserLocation {
    return nil
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top