Search code examples
iosobjective-cannotationsmkmapviewmkannotationview

Map annotation index number keep changing


I wanted to get the map annotation index number when clicking on the map point, but I had one issue which is when I run the app for the first time and click on the point it show me index = 5. But when I re-run the app and click on the same point again it shows me index = 23. The index number keep changing after I re-run the app and it won't show the same index number at the exact point I clicked.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
        if ([view.annotation isKindOfClass:[MKPointAnnotation class]]) {

            NSUInteger index = [mapView.annotations indexOfObject:view.annotation];
            NSLog(@"Index number is %lu", (unsigned long)index);
}

Solution

  • I found the solution, I saved an location ID into annotation.title after that I'm using the code below to replace the index number.

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
            if ([view.annotation isKindOfClass:[MKPointAnnotation class]]) {
               NSString *hubIden = view.annotation.title;
           }
    }