IPhone MKMapView: set span / region to display all contacts on the map

I am working on a project (iOS 7 platform) in which I need a current location with stores of about 5 km, therefore, how to calculate the span / region value to display all stores with the current location on the map.

MKMapRect zoomRect = MKMapRectNull; double inset; for (id <MKAnnotation> annotation in mapVW.annotations) { MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); zoomRect = MKMapRectUnion(zoomRect, pointRect); inset = -zoomRect.size.width * 20; } [mapVW setVisibleMapRect:MKMapRectInset(zoomRect, inset, inset) animated:YES]; 

that's what i am trying

thanks

-one
objective-c
source share
2 answers

It's unclear what your exact problem is, but the following may help:

  • The calculation of the inset looks wrong. It sets the inset (side margins) up to 20 times the width of the entire zoom area. You probably need to set the inset to a small portion of its entire width. Perhaps you meant 0.20 instead of 20.0 :

     inset = -zoomRect.size.width * 0.20; 

    You also don't need to set the inset multiple times inside the for loop, since it depends only on the final width . You can move the above line after the for loop before calling setVisibleMapRect .

  • You mentioned a problem with your current location. It's not clear what the problem is, but maybe you mean that this scaling code does not include the current location? If so, the current location may not yet be determined when this code is called. Try moving this code (or also calling it) from the didUpdateUserLocation delegate didUpdateUserLocation . Make sure that showsUserLocation is YES and that the delegate map view is displayed.


By the way: iOS 7 includes the new showAnnotations:animated: method, which automatically determines the bounding box for some given annotations and sets the visible display area for you. It does not allow you to specify a custom insert as you do (although this is not the case by default). Therefore, instead of this loop, you should:

 [mapVW showAnnotations:mapVW.annotations animated:YES]; 
+3
source share
 NSArray *anno_Arrr = mapview.annotations; [mapview showAnnotations:anno_Arrr animated:YES]; 
+2
source share

All Articles