Search code examples
iphonemapkitmkmapview

UIMapKit - get Annotations from server for different regions


I have this code where I do this

  1. as soon as the map loads with users current location - I give a span to the map
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
  1. Next I send API call with the top left lat, lng and bottom right lat,lng
CLLocationCoordinate2D topLeft, bottomRight;
topLeft = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];
CGPoint pointBottomRight = CGPointMake(mapView.frame.size.width, mapView.frame.size.height);
bottomRight = [mapView convertPoint:pointBottomRight toCoordinateFromView:mapView];
  1. I call this method everytime there is a region change
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

The Problem - I need to get new pins from the API - only when there is a new region and not when the user just zooms in meaning i dotn want multiple pins for the same location.

If I have a huge region say X. and it has A, B, C, D inside it.

Suppose I start with X - say entire globe map - i get pins - now if the user zooms in zooms out or does whatever i never want to call the API coz i have all the pins.

Now If I start with A - juts a map of NY and I zoom in ( no need to call the api) but if I move to region B - say washington DC - i need to call the api.

I hope you understand what i am asking - any suggestions.

Any help would be appreciated.


Solution

  • I would do two different things to handle this:

    First, add an UUID to all your annotations on the server so that you can uniquely identify them. Use the UUID to store your on screen annotations in an NSDictionary.

    Second, when a user zooms, do the request for the zoomed region and check to see if the annotations you downloaded match any of the annotations in your dictionary. If they do match, discard the downloaded data, if they don't match, add them to the map and your dictionary.