开发者

MKAnnotations are being made successfully, however they sometimes fail to render on MKMapView

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 lo开发者_运维问答cations 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?


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:

Optimizing CLLocationManager/CoreLocation to retrieve data points faster on the iPhone

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜