MKMapView地区

时间:2013-09-06 13:13:12

标签: ios mkmapview mkmapviewdelegate

我有一个关于在我的MKMapView上设置区域的问题。

我需要设置mapview以在我的视图首次加载时显示特定区域。

该地区的东北和西南纬度和经度为:

North East Coordinate Lat:59.623724 Long:2.911587
South West Coordinate Lat:49.004833 Long:-11.361825

除此之外,我想将mapview“锁定”到这个区域。理想情况下,锁将是透明的,即:上面的坐标代表MKMapView的最大范围。但是,如果仅仅是检查

内的东北和西南坐标
- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated 

并重置视图,如果它们超出我的最大范围,我也可以接受。

非常感谢有关此问题的任何指示。

编辑: 关于我的问题的第一部分,我已经想到我可以使用以下代码在MKMapView上设置初始区域:

CLLocationCoordinate2D neCoord;
neCoord.latitude = 59.787643;
neCoord.longitude = 3.025857;

CLLocationCoordinate2D swCoord;
swCoord.latitude = 49.394171;
swCoord.longitude = -11.036642;
MKCoordinateRegion region;
region.center.latitude = neCoord.latitude - (neCoord.latitude - swCoord.latitude) * 0.5;
region.center.longitude = neCoord.longitude + (swCoord.longitude - neCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(neCoord.latitude - swCoord.latitude); // Add a little extra space on the sides
region.span.longitudeDelta = fabs(swCoord.longitude - neCoord.longitude); // Add a little extra space on the sides

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

1 个答案:

答案 0 :(得分:0)

首先,您需要确保在显示视图后在地图视图上设置区域。如果在加载地图之前设置它,它可能不会以该区域为中心。完成后,只需设置self.mapView.zoomEnabled = NO;self.mapView.scrollEnabled = NO;即可阻止用户移动地图。

如果要锁定用户可以查看但仍允许滚动和缩放的最大边界,则必须使用-mapView:regionDidChangeAnimated:并在用户离开时将其“撞击”在边界内。请注意,用户体验可能很糟糕 - 他们会平移,放手,然后地图会突然移回您定义的区域。您可以尝试使用-mapView:regionWillChangeAnimated:并修改地图区域(如果它们离开了您的边界),这可能会稍微不那么刺耳。