Reputation: 13344
Below is my code for loading my custom annotations into an array and then adding this array to my map using addAnnotations. I am not getting any pins on my map. If I replace [annotations addObject:annotation];
with [annotations addObject:item.placemark];
I get pins but they are not my custom annotations. I have a customAnnotation class called Annotation. What am I doing wrong?
NSMutableArray *annotations = [NSMutableArray array];
[response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {
// if we already have an annotation for this MKMapItem,
// just return because you don't have to add it again
for (id<MKAnnotation>annotation in mapView.annotations)
{
if (annotation.coordinate.latitude == item.placemark.coordinate.latitude &&
annotation.coordinate.longitude == item.placemark.coordinate.longitude)
{
return;
}
}
// otherwise add it
Annotation *annotation = [[Annotation alloc] initWithPlacemark:item.placemark];
annotation.title = mapItem.name;
annotation.subtitle = mapItem.placemark.addressDictionary[(NSString *)kABPersonAddressStreetKey];
[annotations addObject:annotation];
[self.mapView addAnnotations:annotations];
Upvotes: 0
Views: 92
Reputation: 438102
The problem, quite simply, can only be (a) problem adding the annotations; or (b) problem showing the annotations. I suspected the former initially (given your weird indentation, I suspected you might have some problem with some asynchronous call you didn't share with us), but given that you say that it works fine if you use the placemark instead of the annotation, I have to suspect the problem must rest in the showing of the annotations (namely viewForAnnotation
).
I'd first suggest you do some diagnostic work. You can confirm whether annotations are being added properly by logging the count
of self.mapView.annotations
before and after the addAnnotations
call. I'd suspect (based upon what you've said) you'll see the count go up.
I would then take a look at viewForAnnotation
, and make sure it's handling your Annotation
class properly. Frequently we have if ([annotation isKindOfClass:...)
logic, so maybe you have some issue there. It's hard to say on the basis of the small snippet of code shared with us.
Upvotes: 1