Search code examples
iosannotationsmkannotationviewmkpinannotationview

Telling the difference between UserLocation Pin and user added pins


I am trying to solve a problem that I'm not sure how to solve. In my app, when the mapkit launches, I drop a pin at the user's current location. The mapkit delegates to viewForAnnotation which sets the pin's color which works fine.

The issue I'm running into is I cannot tell the annotations apart so I can apply a different color to "user added locations" vs. my "current location" pin. I would like to add a button so they can delete any pins they add but not be able to delete their "current location" pin. I can't seem to figure out how to extract any identifiable pieces of information like title or subtitle of the pin.

Thanks in advance for your help.

Here's my code...

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    NSLog(@"OAAssignmentGPSViewController.m : mapView viewForAnnotation");

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

    NSLog(@" mapView.userLocation.title = %@", self.mapView.userLocation.title);
    static NSString* annotationIdentifier = @"currentlocation";
    MKPinAnnotationView *myPinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
    if (!myPinView) {
    myPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] ;
    myPinView.pinColor = MKPinAnnotationColorRed;
    myPinView.canShowCallout = YES;
    myPinView.animatesDrop = YES;

    NSLog(@"mapView.description %@", self.mapView.userLocation.title);
    if( annotation   != self.mapView.userLocation)<<<<<----how do I check for UserLocation
    {
        myPinView.pinColor = MKPinAnnotationColorGreen;
        myPinView.rightCalloutAccessoryView =   [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
   }
    return myPinView;
}

Solution

  • Updating this for other newbies, this was the answer...

    if ([annotation isKindOfClass:[MKUserLocation class]]) ....{
    
       this is the current location pin
    
    }else {
    
        these are you custom locations pins
    }