缩放MKMapView以适应注释引脚?

我正在使用MKMapView,并在地图上添加了大量的注释引脚,大约有5-10公里的区域。 当我运行应用程序时,我的地图开始缩小以显示整个世界,缩放地图的最佳方式是什么,以便引脚适合视图?

编辑:我最初的想法是使用MKCoordinateRegionMake并从我的注释计算坐标中心,经度Delta和纬度Delta。 我很确定这会起作用,但我只是想检查我没有失去任何明显的东西。

添加代码,BTW:FGLocation是一个符合MKAnnotation的类,locationFake是这些对象的一个NSMutableArray 。 总是欢迎评论….

 - (MKCoordinateRegion)regionFromLocations { CLLocationCoordinate2D upper = [[locationFake objectAtIndex:0] coordinate]; CLLocationCoordinate2D lower = [[locationFake objectAtIndex:0] coordinate]; // FIND LIMITS for(FGLocation *eachLocation in locationFake) { if([eachLocation coordinate].latitude > upper.latitude) upper.latitude = [eachLocation coordinate].latitude; if([eachLocation coordinate].latitude < lower.latitude) lower.latitude = [eachLocation coordinate].latitude; if([eachLocation coordinate].longitude > upper.longitude) upper.longitude = [eachLocation coordinate].longitude; if([eachLocation coordinate].longitude < lower.longitude) lower.longitude = [eachLocation coordinate].longitude; } // FIND REGION MKCoordinateSpan locationSpan; locationSpan.latitudeDelta = upper.latitude - lower.latitude; locationSpan.longitudeDelta = upper.longitude - lower.longitude; CLLocationCoordinate2D locationCenter; locationCenter.latitude = (upper.latitude + lower.latitude) / 2; locationCenter.longitude = (upper.longitude + lower.longitude) / 2; MKCoordinateRegion region = MKCoordinateRegionMake(locationCenter, locationSpan); return region; } 

你说得对。

find你的最大和最小的纬度和经度,应用一些简单的算术,并使用MKCoordinateRegionMake

对于iOS 7及以上版本,使用showAnnotations:animated:MKMapView.h

 // Position the map such that the provided array of annotations are all visible to the fullest extent possible. - (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9, 7_0); 

这是我在这里find的那个为我工作的人:

(编辑:我已经更新了解决scheme使用@ Micah的build议增加0.1的pointRect,以确保矩形不会无限小!)

 MKMapRect zoomRect = MKMapRectNull; for (id <MKAnnotation> annotation in mapView.annotations) { MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); zoomRect = MKMapRectUnion(zoomRect, pointRect); } [mapView setVisibleMapRect:zoomRect animated:YES]; 

您也可以通过将第一行replace为更新来包含userLocation引脚:

 MKMapPoint annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate); MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); 

苹果已经为IOS 7增加了一个新的方法来简化生活。

 [mapView showAnnotations:yourAnnotationArray animated:YES]; 

您可以轻松地从存储在地图视图中的数组中拉出:

 yourAnnotationArray = mapView.annotations; 

并快速调整相机呢!

 mapView.camera.altitude *= 1.4; 

除非用户安装了iOS 7+或OS X 10.9+,否则这将不起作用。 在这里检查自定义animation

我使用这个代码,对我来说工作得很好:

 -(void)zoomToFitMapAnnotations:(MKMapView*)aMapView { if([aMapView.annotations count] == 0) return; CLLocationCoordinate2D topLeftCoord; topLeftCoord.latitude = -90; topLeftCoord.longitude = 180; CLLocationCoordinate2D bottomRightCoord; bottomRightCoord.latitude = 90; bottomRightCoord.longitude = -180; for(MapViewAnnotation *annotation in mapView.annotations) { topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); } MKCoordinateRegion region; region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides region = [aMapView regionThatFits:region]; [mapView setRegion:region animated:YES]; } 

@ jowie的解决scheme很好。 一个捕获,如果一个地图只有一个注释,你将会得到一个完全缩小的地图。 我在rect make size中加了0.1,以确保setVisibleMapRect有一些可以放大的东西。

 MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); 

在Swift中使用

 mapView.showAnnotations(annotationArray, animated: true) 

在目标c

 [mapView showAnnotations:annotationArray animated:YES]; 

我已经改变了拉斐尔莫雷拉的答案。 信用归功于他。 对于那些寻找Swift版本的人来说,这里是代码:

  func zoomToFitMapAnnotations(aMapView: MKMapView) { guard aMapView.annotations.count > 0 else { return } var topLeftCoord: CLLocationCoordinate2D = CLLocationCoordinate2D() topLeftCoord.latitude = -90 topLeftCoord.longitude = 180 var bottomRightCoord: CLLocationCoordinate2D = CLLocationCoordinate2D() bottomRightCoord.latitude = 90 bottomRightCoord.longitude = -180 for annotation: MKAnnotation in myMap.annotations as! [MKAnnotation]{ topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude) topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude) bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude) bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude) } var region: MKCoordinateRegion = MKCoordinateRegion() region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5 region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5 region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.4 region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.4 region = aMapView.regionThatFits(region) myMap.setRegion(region, animated: true) } 

如果您正在寻找iOS 8及以上版本 ,最简单的方法是在调用func showAnnotations(annotations: [MKAnnotation], animated: Bool)之前var layoutMargins: UIEdgeInsets { get set }您的地图视图的var layoutMargins: UIEdgeInsets { get set }

比如(Swift 2.1):

 @IBOutlet weak var map: MKMapView! { didSet { map.delegate = self map.mapType = .Standard map.pitchEnabled = false map.rotateEnabled = false map.scrollEnabled = true map.zoomEnabled = true } } // call 'updateView()' when viewWillAppear or whenever you set the map annotations func updateView() { map.layoutMargins = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25) map.showAnnotations(map.annotations, animated: true) } 

在for循环中添加了这个If循环来排除这个方法中的用户位置引脚(在我的例子中也许是其他的)

 if (![annotation isKindOfClass:[MKUserLocation class]] ) { //Code Here... } 

对于iOS 7及以上版本(请参阅MKMapView.h):

 // Position the map such that the provided array of annotations are all visible to the fullest extent possible. - (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9, 7_0); 

来自 – Abhishek Bedi的评论

你只要打电话:

  [yourMapView showAnnotations:@[yourAnnotation] animated:YES]; 

我创build了一个扩展来显示所有的注释,使用swift 2.3中的一些代码。 如果即使在最大缩放级别下也不能显示所有注释。

 import MapKit extension MKMapView { func fitAllAnnotations() { var zoomRect = MKMapRectNull; for annotation in annotations { let annotationPoint = MKMapPointForCoordinate(annotation.coordinate) let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); zoomRect = MKMapRectUnion(zoomRect, pointRect); } setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(20, 20, 20, 20), animated: true) } } 

Swift 3这是适合地图中所有注释的正确方法。

 func zoomMapaFitAnnotations() { var zoomRect = MKMapRectNull for annotation in mapview.annotations { let annotationPoint = MKMapPointForCoordinate(annotation.coordinate) let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0) if (MKMapRectIsNull(zoomRect)) { zoomRect = pointRect } else { zoomRect = MKMapRectUnion(zoomRect, pointRect) } } self.mapview.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(50, 50, 50, 50), animated: true) } 

在Swift中

  var zoomRect = MKMapRectNull; for i in 0..<self.map.annotations.count { let annotation: MKAnnotation = self.map.annotations[i] let annotationPoint = MKMapPointForCoordinate(annotation.coordinate); let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); zoomRect = MKMapRectUnion(zoomRect, pointRect); } self.map.setVisibleMapRect(zoomRect, animated: true) 

感谢jowie,我已经将我的旧类更新为更优雅的解决scheme。 共享完整的,几乎复制和粘贴就绪解决scheme

的MKMapView + AnnotationsRegion.h

 #import <MapKit/MapKit.h> @interface MKMapView (AnnotationsRegion) -(void)updateRegionForCurrentAnnotationsAnimated:(BOOL)animated; -(void)updateRegionForCurrentAnnotationsAnimated:(BOOL)animated edgePadding:(UIEdgeInsets)edgePadding; -(void)updateRegionForAnnotations:(NSArray *)annotations animated:(BOOL)animated; -(void)updateRegionForAnnotations:(NSArray *)annotations animated:(BOOL)animated edgePadding:(UIEdgeInsets)edgePadding; @end 

的MKMapView + AnnotationsRegion.m

 #import "MKMapView+AnnotationsRegion.h" @implementation MKMapView (AnnotationsRegion) -(void)updateRegionForCurrentAnnotationsAnimated:(BOOL)animated{ [self updateRegionForCurrentAnnotationsAnimated:animated edgePadding:UIEdgeInsetsZero]; } -(void)updateRegionForCurrentAnnotationsAnimated:(BOOL)animated edgePadding:(UIEdgeInsets)edgePadding{ [self updateRegionForAnnotations:self.annotations animated:animated edgePadding:edgePadding]; } -(void)updateRegionForAnnotations:(NSArray *)annotations animated:(BOOL)animated{ [self updateRegionForAnnotations:annotations animated:animated edgePadding:UIEdgeInsetsZero]; } -(void)updateRegionForAnnotations:(NSArray *)annotations animated:(BOOL)animated edgePadding:(UIEdgeInsets)edgePadding{ MKMapRect zoomRect = MKMapRectNull; for(id<MKAnnotation> annotation in annotations){ MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); zoomRect = MKMapRectUnion(zoomRect, pointRect); } [self setVisibleMapRect:zoomRect edgePadding:edgePadding animated:animated]; } @end 

希望它能帮助别人,再次感谢jowie!

  - (void)zoomMapViewToFitAnnotationsWithExtraZoomToAdjust:(double)extraZoom { if ([self.annotations count] == 0) return; int i = 0; MKMapPoint points[[self.annotations count]]; for (id<MKAnnotation> annotation in [self annotations]) { points[i++] = MKMapPointForCoordinate(annotation.coordinate); } MKPolygon *poly = [MKPolygon polygonWithPoints:points count:i]; MKCoordinateRegion r = MKCoordinateRegionForMapRect([poly boundingMapRect]); r.span.latitudeDelta += extraZoom; r.span.longitudeDelta += extraZoom; [self setRegion: r animated:YES]; } 
  var zoomRect: MKMapRect = MKMapRectNull for annotation in mapView.annotations { let annotationPoint = MKMapPointForCoordinate(annotation.coordinate) let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1) zoomRect = MKMapRectUnion(zoomRect, pointRect) } mapView.setVisibleMapRect(zoomRect, animated: true) 

本页面上的所有答案都假定地图占据了整个屏幕 。 我实际上有一个HUD显示(即顶部和底部分散的button),在地图顶部提供信息。因此,页面上的algorithm将显示引脚正确,但其中一些将显示 HUD显示button下。

我的解决scheme将地图放大到在屏幕的子集中显示注释,并适用于不同的屏幕尺寸 (即3.5“vs 4.0”等):

 // create a UIView placeholder and throw it on top of the original mapview // position the UIView to fit the maximum area not hidden by the HUD display buttons // add an *other* mapview in that uiview, // get the MKCoordinateRegion that fits the pins from that fake mapview // kill the fake mapview and set the region of the original map // to that MKCoordinateRegion. 

这里是我在代码中做的(注意:我使用NSConstraints和一些辅助方法来使我的代码在不同的屏幕尺寸下工作..而代码是非常可读的..我的答案在这里解释更好..它基本上是相同的工作stream程: )

 // position smallerMap to fit available space // don't store this map, it will slow down things if we keep it hidden or even in memory [@[_smallerMapPlaceholder] mapObjectsApplyingBlock:^(UIView *view) { [view removeFromSuperview]; [view setTranslatesAutoresizingMaskIntoConstraints:NO]; [view setHidden:NO]; [self.view addSubview:view]; }]; NSDictionary *buttonBindingDict = @{ @"mapPlaceholder": _smallerMapPlaceholder}; NSArray *constraints = [@[@"V:|-225-[mapPlaceholder(>=50)]-176-|", @"|-40-[mapPlaceholder(<=240)]-40-|" ] mapObjectsUsingBlock:^id(NSString *formatString, NSUInteger idx){ return [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:buttonBindingDict]; }]; [self.view addConstraints:[constraints flattenArray]]; [self.view layoutIfNeeded]; MKMapView *smallerMap = [[MKMapView alloc] initWithFrame:self.smallerMapPlaceholder.frame]; [_smallerMapPlaceholder addSubview:smallerMap]; MKCoordinateRegion regionThatFits = [smallerMap getRegionThatFits:self.mapView.annotations]; [smallerMap removeFromSuperview]; smallerMap = nil; [_smallerMapPlaceholder setHidden:YES]; [self.mapView setRegion:regionThatFits animated:YES]; 

这里是适合的区域的代码:

 - (MKCoordinateRegion)getRegionThatFits:(NSArray *)routes { MKCoordinateRegion region; CLLocationDegrees maxLat = -90.0; CLLocationDegrees maxLon = -180.0; CLLocationDegrees minLat = 90.0; CLLocationDegrees minLon = 180.0; for(int idx = 0; idx < routes.count; idx++) { CLLocation* currentLocation = [routes objectAtIndex:idx]; if(currentLocation.coordinate.latitude > maxLat) maxLat = currentLocation.coordinate.latitude; if(currentLocation.coordinate.latitude < minLat) minLat = currentLocation.coordinate.latitude; if(currentLocation.coordinate.longitude > maxLon) maxLon = currentLocation.coordinate.longitude; if(currentLocation.coordinate.longitude < minLon) minLon = currentLocation.coordinate.longitude; } region.center.latitude = (maxLat + minLat) / 2.0; region.center.longitude = (maxLon + minLon) / 2.0; region.span.latitudeDelta = 0.01; region.span.longitudeDelta = 0.01; region.span.latitudeDelta = ((maxLat - minLat)<0.0)?100.0:(maxLat - minLat); region.span.longitudeDelta = ((maxLon - minLon)<0.0)?100.0:(maxLon - minLon); MKCoordinateRegion regionThatFits = [self regionThatFits:region]; return regionThatFits; } 

正如阿布舍克·贝迪(Abhishek Bedi)在评论中指出的,对于iOS7来说,最好的方法是:

 //from API docs: //- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9, 7_0); [self.mapView showAnnotations:self.mapView.annotations animated:YES]; 

对于我的个人项目(在iOS7之前),我只是在MKMapView类中添加一个类来封装“可见区域”function,用于一个非常常见的操作:将其设置为能够查看MKMapView实例上所有当前加载的注释这包括尽可能多的引脚,以及用户的位置)。 结果是这样的:

.h文件

 #import <MapKit/MapKit.h> @interface MKMapView (Extensions) -(void)ij_setVisibleRectToFitAllLoadedAnnotationsAnimated:(BOOL)animated; -(void)ij_setVisibleRectToFitAnnotations:(NSArray *)annotations animated:(BOOL)animated; @end 

.m文件

 #import "MKMapView+Extensions.h" @implementation MKMapView (Extensions) /** * Changes the currently visible portion of the map to a region that best fits all the currently loadded annotations on the map, and it optionally animates the change. * * @param animated is the change should be perfomed with an animation. */ -(void)ij_setVisibleRectToFitAllLoadedAnnotationsAnimated:(BOOL)animated { MKMapView * mapView = self; NSArray * annotations = mapView.annotations; [self ij_setVisibleRectToFitAnnotations:annotations animated:animated]; } /** * Changes the currently visible portion of the map to a region that best fits the provided annotations array, and it optionally animates the change. All elements from the array must conform to the <MKAnnotation> protocol in order to fetch the coordinates to compute the visible region of the map. * * @param annotations an array of elements conforming to the <MKAnnotation> protocol, holding the locations for which the visible portion of the map will be set. * @param animated wether or not the change should be perfomed with an animation. */ -(void)ij_setVisibleRectToFitAnnotations:(NSArray *)annotations animated:(BOOL)animated { MKMapView * mapView = self; MKMapRect r = MKMapRectNull; for (id<MKAnnotation> a in annotations) { ZAssert([a conformsToProtocol:@protocol(MKAnnotation)], @"ERROR: All elements of the array MUST conform to the MKAnnotation protocol. Element (%@) did not fulfill this requirement", a); MKMapPoint p = MKMapPointForCoordinate(a.coordinate); //MKMapRectUnion performs the union between 2 rects, returning a bigger rect containing both (or just one if the other is null). here we do it for rects without a size (points) r = MKMapRectUnion(r, MKMapRectMake(px, py, 0, 0)); } [mapView setVisibleMapRect:r animated:animated]; } @end 

正如你所看到的,到目前为止,我已经添加了2个方法:一个用于将映射的可见区域设置为适合MKMapView实例上所有当前加载的批注的可见区域,另一个将其设置为任何对象数组。 因此,要设置mapView的可见区域,代码将如下所示:

  //the mapView instance [self.mapView ij_setVisibleRectToFitAllLoadedAnnotationsAnimated:animated]; 

我希望它有助于=)

我已经做了一些Rafael MKMapView类代码修改。

 - (void)zoomToFitMapAnnotations { if ([self.annotations count] == 0) return; CLLocationCoordinate2D topLeftCoord; topLeftCoord.latitude = -90; topLeftCoord.longitude = 180; CLLocationCoordinate2D bottomRightCoord; bottomRightCoord.latitude = 90; bottomRightCoord.longitude = -180; for (id <MKAnnotation> annotation in self.annotations) { topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); } MKCoordinateRegion region; region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides [self setRegion:[self regionThatFits:region] animated:YES]; } 

只是分享我的意见:

如果您在故事板中使用带有“推测”尺寸的xCode> 6(请参阅文件检查器上的“模拟度量”),则调用

- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated

viewDidLoad将导致4英寸iPhone上的太大的缩放级别,因为地图的布局仍然在故事板的更大的屏幕的大小。

您可以将您的电话转到showAnnotations...showAnnotations...是否出现。 然后地图的大小已经调整到iPhone 4的较小的屏幕。

或者,也可以将“模拟度量”下文件检查器中的“推断”值更改为iphone 4英寸。

其他方式在Swft 3

 func zoomMapaFitAnnotations() { var zoomRect = MKMapRectNull for annotation in mapview.annotations { let annotationPoint = MKMapPointForCoordinate(annotation.coordinate) let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0) if (MKMapRectIsNull(zoomRect)) { zoomRect = pointRect } else { zoomRect = MKMapRectUnion(zoomRect, pointRect) } } self.mapview.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(50, 50, 50, 50), animated: true) } 

@“我不知道这是否是因为我的实现中的其他因素,但是我发现showAnnotations不像手动实现那么贴近缩放/贴合注释,所以我坚持使用手册之一 – 泰德艾利4月17日在0:35“

我有同样的问题,但后来我试图做两次showAnnotations(如下图),由于某种原因,它的工作。

[mapView showAnnotations:yourAnnotationArray animated:YES]; [mapView showAnnotations:yourAnnotationArray animated:YES];

与iOS 7兼容的方式是使用以下内容。 首先调用showAnnotation以获得包含所有注释的矩形。 之后创build和UIEdgeInset顶端插入的高度。 因此,你确保显示在地图上的整个销。

 [self.mapView showAnnotations:self.mapView.annotations animated:YES]; MKMapRect rect = [self.mapView visibleMapRect]; UIEdgeInsets insets = UIEdgeInsetsMake(pinHeight, 0, 0, 0); [self.mapView setVisibleMapRect:rect edgePadding:insets animated:YES]; 

根据上面的答案,您可以使用通用方法缩放地图,以同时适合所有注释和叠加。

 -(MKMapRect)getZoomingRectOnMap:(MKMapView*)map toFitAllOverlays:(BOOL)overlays andAnnotations:(BOOL)annotations includeUserLocation:(BOOL)userLocation { if (!map) { return MKMapRectNull; } NSMutableArray* overlaysAndAnnotationsCoordinateArray = [[NSMutableArray alloc]init]; if (overlays) { for (id <MKOverlay> overlay in map.overlays) { MKMapPoint overlayPoint = MKMapPointForCoordinate(overlay.coordinate); NSArray* coordinate = @[[NSNumber numberWithDouble:overlayPoint.x], [NSNumber numberWithDouble:overlayPoint.y]]; [overlaysAndAnnotationsCoordinateArray addObject:coordinate]; } } if (annotations) { for (id <MKAnnotation> annotation in map.annotations) { MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); NSArray* coordinate = @[[NSNumber numberWithDouble:annotationPoint.x], [NSNumber numberWithDouble:annotationPoint.y]]; [overlaysAndAnnotationsCoordinateArray addObject:coordinate]; } } MKMapRect zoomRect = MKMapRectNull; if (userLocation) { MKMapPoint annotationPoint = MKMapPointForCoordinate(map.userLocation.coordinate); zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); } for (NSArray* coordinate in overlaysAndAnnotationsCoordinateArray) { MKMapRect pointRect = MKMapRectMake([coordinate[0] doubleValue], [coordinate[1] doubleValue], 0.1, 0.1); zoomRect = MKMapRectUnion(zoomRect, pointRect); } return zoomRect; } 

接着:

 MKMapRect mapRect = [self getZoomingRectOnMap:mapView toFitAllOverlays:YES andAnnotations:YES includeUserLocation:NO]; [mapView setVisibleMapRect:mapRect edgePadding:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0) animated:YES]; 

把这个相应的代码:

  - (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views { id<MKAnnotation> mp = [annotationView annotation]; MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,250,250); [mv setRegion:region animated:YES]; }