-1

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.

Gil Sand
  • 5,802
  • 5
  • 36
  • 78

2 Answers2

1

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;
}
Rick
  • 1,818
  • 1
  • 15
  • 18
  • Oh thanks, i don't know how i didn't think of that. Sometimes, i guess i'm just stuck with an idea and can't see anywhere else ! Good call ! – Gil Sand Jul 28 '14 at 14:42
0

You can subclass MKPointAnnotation and create a new property with your object

@interface CustomPointAnnotation : MKPointAnnotation

@property (nonatomic, strong) CustomObject* object;

@end

Now whenever you deal with an <MKAnnotation>, cast it to a CustomPointAnnotation* and set/get the object property.

SomeGuy
  • 9,670
  • 3
  • 32
  • 35