Search code examples
iosmkmapviewmkannotationuser-location

Locate the user's location in a map with multiple pins


I need to see user location using a custom annotation, and then I add more annotaions of various types in the vicinity of the user (eg monuments, restaurants cinema ...). If I write the code in this method displays the user's location.

- (void) locationManager:(CLLocationManager *) manager
 didUpdateToLocation:(CLLocation *) newLocation
        fromLocation:(CLLocation *) oldLocation {

    //......

   MyAnnotation *annotationUser = [[MyAnnotation alloc]initWithCoordinates:self.coordinate  title:@"YOU ARE HERE" subTitle:@"via Giorgio Giulini 2"];

   [self.mapView addAnnotation:self.annotationUser];


}

If I add the method

 - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
 {
      //.....
  }

to create the other annotations, the user's location is no longer displayed. Why?


Solution

  • try this

        - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
     {
        if ([annotation isKindOfClass:[MyAnnotation class]]) {
    
            return nil;
    
        } else {
    
            //.....
    
        }
    
    
      }