Iphone 在MKMapView中设置区域

Iphone 在MKMapView中设置区域,iphone,ios,objective-c,mkmapview,Iphone,Ios,Objective C,Mkmapview,我有10-15拉特长显示在地图上的时间。我应该如何设置区域以便可以看到地图上的所有注释。尝试这样做可能会对您有所帮助 MKCoordinateSpan span = MKCoordinateSpanMake(52.0f,80.0001f); CLLocationCoordinate2D coordinate = {8.9999, 18.2812}; MKCoordinateRegion region = {coordinate, span};

我有10-15拉特长显示在地图上的时间。我应该如何设置区域以便可以看到地图上的所有注释。

尝试这样做可能会对您有所帮助

        MKCoordinateSpan span = MKCoordinateSpanMake(52.0f,80.0001f);
        CLLocationCoordinate2D coordinate = {8.9999, 18.2812};
        MKCoordinateRegion region = {coordinate, span};

        MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region];
        NSLog(@"Fit Region %f %f", regionThatFits.center.latitude, regionThatFits.center.longitude);

        [self.mapView setRegion:regionThatFits animated:YES];

分别替换坐标和所有值。

对于“显示所有注释”使用此方法,我在与您的相同版本中使用了此方法。这对我有用

-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
{
    if([mapView.annotations count] == 0)
        return;

    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for(MYAnnotation* annotation in mapView.annotations)// here MYAnnotation is custom annotation call.
    {
        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);

    }

    NSLog(@"A%f, B%f, C%f, D%f,", topLeftCoord.latitude, topLeftCoord.longitude, bottomRightCoord.latitude, bottomRightCoord.longitude);

    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;
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;

    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:YES];
}
在地图视图上设置所有位置(注释)后调用此方法,如

 [self zoomToFitMapAnnotations:self.mapView];
创建类,如下所示。创建两个类:ViewController是UIViewController的类,MapViewAnnotation是NSObject的类。我创建了两个类,如下所示。在ViewController的XIB中绑定mapview并设置委托。
#进口
#进口
#导入“MapViewAnnotation.h”
@界面ViewController:UIViewController{
IBMKMAPVIEW*mapView;
NSMutableArray*数组位置;
}
@结束
#导入“ViewController.h”
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
arrayLocation=[[NSMutableArray alloc]init];
NSMutableArray*arrAnnotations=[[NSMutableArray alloc]init];
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];
[dict setValue:@“32.774125”forKey:@“纬度”];
[dict setValue:@“-117.240658”forKey:@“经度”];
[dict setValue:@“酒吧和餐厅1”forKey:@“标题”];
[arrayLocation addObject:dict];
dict=nil;
dict=[[NSMutableDictionary alloc]init];
[dict setValue:@“32.784526”forKey:@“纬度”];
[dict setValue:@“-117.240985”forKey:@“经度”];
[dict setValue:@“酒吧和餐厅2”forKey:@“标题”];
[arrayLocation addObject:dict];
dict=nil;
dict=[[NSMutableDictionary alloc]init];
[dict setValue:@“32.773477”forKey:@“纬度”];
[dict setValue:@“-117.241144”forKey:@“经度”];
[dict setValue:@“酒吧和餐厅3”forKey:@“标题”];
[arrayLocation addObject:dict];
dict=nil;
dict=[[NSMutableDictionary alloc]init];
[dict setValue:@“32.775301”forKey:@“纬度”];
[dict setValue:@“-117.238893”forKey:@“经度”];
[dict setValue:@“酒吧和餐厅4”forKey:@“标题”];
[arrayLocation addObject:dict];
dict=nil;
对于(int i=0;i maxLat)maxLat=vu.coordinate.lation;
如果(vu.coordinate.longitudemaxLon)maxLon=vu.coordinate.longitude;
}
CLLocationCoordinate2D中心=CLLocationCoordinate2DMake((minLat+maxLat)/2.0,(minLon+maxLon)/2.0);
MKCoordinateSpan span=MKCoordinateSpanMake(maxLat-minLat,maxLon-minLon);
MKCoordinateRegion区域=MKCoordinateRegionMake(中心,跨度);
返回区;
}
@结束

如果有人在寻找Swift 3解决方案,我已经编写了这个对我来说非常有效的快速扩展:)

它允许您快速执行以下操作:

map.fitCoordinates([userLocation, location1, location2])
希望这对任何人都有帮助


要点:你为什么不接受答案?这是一件体面的事情。@Amit:接受这个对你清理社区有用的答案。
public extension MKMapView {
    public static func visibleRect(for coords: [CLLocationCoordinate2D]) -> MKMapRect {
        return coords.reduce(MKMapRectNull) { outRect, coord in
            let point = MKMapPointForCoordinate(coord)
            let rect = MKMapRectMake(point.x, point.y, 0.1, 0.1)
            let union = MKMapRectUnion(rect, outRect)

            return union
        }
    }

    public func fitCoordinates(_ coords: [CLLocationCoordinate2D],
                               animated: Bool = true,
                               insets: UIEdgeInsets = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25)) {
        let rect = MKMapView.visibleRect(for: coords)
        self.setVisibleMapRect(rect, edgePadding: insets, animated: animated)
    }
}
map.fitCoordinates([userLocation, location1, location2])