views:

316

answers:

1

I'm working on an iPhone app using the 3.1.3 SDK, my app finds the users current location, displays it on a MKMapView and then finds nearby locations and renders them as MKAnnotations. My code is working, however sometimes the nearby annotations do not appear on the map. They are still being made as I see the correct data in the console (from NSLog which runs just after the annotations are made).

When it fails is completely random, it could be the 5th time I've hit "Build and Run" for the day, or the 500th it doesn't appear to have any pattern and doesn't throw any type of error, it just doesn't add the annotations to the MapView.

This is the method called for each nearby location to add the MKAnnotation.

- (void)addPinsWithLocation:(NSDictionary *)spot
{
    CLLocationCoordinate2D location;
    location.longitude = [[spot objectForKey:@"spot_longitude"] doubleValue];
    location.latitude = [[spot objectForKey:@"spot_latitude"] doubleValue];

    PlaceMarks *placemark = [[PlaceMarks alloc] initWithCoordinate:location title:[spot objectForKey:@"spot_name"] subtitle:@""];
    NSLog(@"Adding Pin for Location: '%@' at %f, %f", [spot objectForKey:@"spot_name"], location.latitude, location.longitude);
    [mapView addAnnotation:placemark];
}

Any ideas on how to get MKAnnotations to always show?

A: 

I'm presently making an app that wants to show all the visible annotations - I would try setting your map view delegate then implementing the following method:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views

Here you can figure out what annotations were added and for example zoom the map to show them - this blog gives you some source code to do this zoom-to-fit:

http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/

Here's some good tips for improving your location information:

http://stackoverflow.com/questions/1081219/optimizing-cllocationmanager-corelocation-to-retrieve-data-points-faster-on-the-i

petert

related questions