Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an improvement that takes into account the height of the annotation views that you are overlaying onto the map (such that the top of the annotation does not get cut off when its coordinate offset is at the bottom for example). Or to generalise further, allows you to specify padding in pixels as opposed to as a percentage. It requires a two stage pass whereby you find out the bounds for the annotations, then you further increase the bounds to take into account your map padding.</p> <pre><code>- (void) zoomToAnnotationsBounds:(NSArray *)annotations { CLLocationDegrees minLatitude = DBL_MAX; CLLocationDegrees maxLatitude = -DBL_MAX; CLLocationDegrees minLongitude = DBL_MAX; CLLocationDegrees maxLongitude = -DBL_MAX; for (MyAnnotation *annotation in annotations) { double annotationLat = annotation.coordinate.latitude; double annotationLong = annotation.coordinate.longitude; minLatitude = fmin(annotationLat, minLatitude); maxLatitude = fmax(annotationLat, maxLatitude); minLongitude = fmin(annotationLong, minLongitude); maxLongitude = fmax(annotationLong, maxLongitude); } // See function below [self setMapRegionForMinLat:minLatitude minLong:minLongitude maxLat:maxLatitude maxLong:maxLongitude]; // If your markers were 40 in height and 20 in width, this would zoom the map to fit them perfectly. Note that there is a bug in mkmapview's set region which means it will snap the map to the nearest whole zoom level, so you will rarely get a perfect fit. But this will ensure a minimum padding. UIEdgeInsets mapPadding = UIEdgeInsetsMake(40.0, 10.0, 0.0, 10.0); CLLocationCoordinate2D relativeFromCoord = [self.mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:self.mapView]; // Calculate the additional lat/long required at the current zoom level to add the padding CLLocationCoordinate2D topCoord = [self.mapView convertPoint:CGPointMake(0, mapPadding.top) toCoordinateFromView:self.mapView]; CLLocationCoordinate2D rightCoord = [self.mapView convertPoint:CGPointMake(0, mapPadding.right) toCoordinateFromView:self.mapView]; CLLocationCoordinate2D bottomCoord = [self.mapView convertPoint:CGPointMake(0, mapPadding.bottom) toCoordinateFromView:self.mapView]; CLLocationCoordinate2D leftCoord = [self.mapView convertPoint:CGPointMake(0, mapPadding.left) toCoordinateFromView:self.mapView]; double latitudeSpanToBeAddedToTop = relativeFromCoord.latitude - topCoord.latitude; double longitudeSpanToBeAddedToRight = relativeFromCoord.latitude - rightCoord.latitude; double latitudeSpanToBeAddedToBottom = relativeFromCoord.latitude - bottomCoord.latitude; double longitudeSpanToBeAddedToLeft = relativeFromCoord.latitude - leftCoord.latitude; maxLatitude = maxLatitude + latitudeSpanToBeAddedToTop; minLatitude = minLatitude - latitudeSpanToBeAddedToBottom; maxLongitude = maxLongitude + longitudeSpanToBeAddedToRight; minLongitude = minLongitude - longitudeSpanToBeAddedToLeft; [self setMapRegionForMinLat:minLatitude minLong:minLongitude maxLat:maxLatitude maxLong:maxLongitude]; } -(void) setMapRegionForMinLat:(double)minLatitude minLong:(double)minLongitude maxLat:(double)maxLatitude maxLong:(double)maxLongitude { MKCoordinateRegion region; region.center.latitude = (minLatitude + maxLatitude) / 2; region.center.longitude = (minLongitude + maxLongitude) / 2; region.span.latitudeDelta = (maxLatitude - minLatitude); region.span.longitudeDelta = (maxLongitude - minLongitude); // MKMapView BUG: this snaps to the nearest whole zoom level, which is wrong- it doesn't respect the exact region you asked for. See http://stackoverflow.com/questions/1383296/why-mkmapview-region-is-different-than-requested [self.mapView setRegion:region animated:YES]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload