Search code examples
iosarraysdictionarymkpointannotation

Getting object from pin selection


I'm using an array of Locations (stored online) which have a LocationID, lat, long, name, PinNumber and UserId.

Steps : I load the complete locations array of the selected user I create pins with that array ( a simple for loop that uses the name, location, etc.)

Sadly, the MKPointAnnotation can only have a name and coordinates, and this is where my problems appears.

When my user selects a pin and uses the annotation (correct me if i'm wrong, that is the little info button inside the selected pin), he is redirected to another page where he can edit that location, and I can't find it in the database because i can't get the ID of the location.

I tried using NSInteger index = [mapView.annotations indexOfObject:view.annotation]; and check that index in my location array, but it just doesn't match.

What can i do to get my object back from that pin ? Or any workaround that gets the job done really.


Solution

  • You can subclass MKAnnotation and add your object ID to it as follows:

    In CustomAnnotation.h,

    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    
    @interface CustomAnnotation : NSObject <MKAnnotation>
    
    @property (nonatomic, retain) NSString *title;
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    @property (nonatomic, retain) NSNumber *objectID;
    
    - (id)initWithTitle:(NSString *)newTitle id:(NSNumber *)objectID location:(CLLocationCoordinate2D)location;
    - (MKAnnotationView *)annotationView;
    @end
    

    In CustomAnnotation.m,

    #import "CustomAnnotation.h"
    
    @implementation CustomAnnotation
    
    - (id)initWithTitle:(NSString *)newTitle id:(NSNumber *)objectID location:(CLLocationCoordinate2D)location
    {
        self = [super init];
        if(self)
        {
            // Initialization code
            _title = newTitle;
            _coordinate = location;
            _objectID = objectID;
        }
        return self;
    }
    
    - (MKAnnotationView *)annotationView
    {
        MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"MyCustomAnnotation"];
    
        // Your settings
        annotationView.draggable = NO;
        annotationView.enabled = YES;
    
        return annotationView;
    }
    @end
    

    Also, in mapView:viewForAnnotation:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        // Customise all annotations except MKUserLocation
        if([annotation isKindOfClass:[CustomAnnotation class]])
        {
            CustomAnnotation *point = (CustomAnnotation *)annotation;
            MKAnnotationView *pointView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"MyCustomAnnotation"];
    
            if(pointView == nil)
                pointView = point.annotationView;
            else
                pointView.annotation = annotation;
    
            ...
            // do something with point.objectID
    
            return pointView;
        }
        else
            return nil;
    }