我已经创建了这个链接上显示的地图,并且它工作得很好。
但问题是,它只能以一种方式放大(只会变大)。我怎样才能让它以任何一种方式工作?
可能就像我们在谷歌地图上的一样(加号 - 左边的棒子)。
对于放大
-(void)zoomIn
{
region.span.latitudeDelta = region.span.latitudeDelta/4 ;
region.span.longitudeDelta = region.span.longitudeDelta/4;
region.center.latitude = mapView.centerCoordinate.latitude ;
region.center.longitude = mapView.centerCoordinate.longitude ;
[mapView setRegion:region animated:YES];
}
对于缩小
-(void)zoomOut
{
region.span.latitudeDelta = region.span.latitudeDelta*4 ;
region.span.longitudeDelta = region.span.longitudeDelta*4;
region.center.latitude = mapView.centerCoordinate.latitude ;
region.center.longitude = mapView.centerCoordinate.longitude ;
[mapView setRegion:region animated:YES];
}
您使用此结构中的增量值来指示地图的所需缩放级别,较小的增量值对应于较高的缩放级别。
请参阅此链接了解更多信息。
这是 Midhun VP 和 Slavco Petkovski 的答案的修订版,当设置 latitudeDelta 太大时,它避免了 zoomOut 中“无效区域”的 NSInvalidArgumentException。放大似乎没有任何问题 - 一旦达到极限,它就不再放大了。
- (void)zoomIn {
MKCoordinateRegion region = [self.mapView region];
region.span.latitudeDelta = region.span.latitudeDelta/4;
region.span.longitudeDelta = region.span.longitudeDelta/4;
region.center.latitude = self.mapView.centerCoordinate.latitude;
region.center.longitude = self.mapView.centerCoordinate.longitude;
[self.mapView setRegion:region animated:YES];
//NSLog(@"zoomIn: center %lf, %lf; spanDelta %lf, %lf, upper left %lf, %lf; lower right %lf %lf", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, region.center.latitude + region.span.latitudeDelta / 2, region.center.longitude - region.span.longitudeDelta / 2, region.center.latitude - region.span.latitudeDelta / 2, region.center.longitude + region.span.longitudeDelta / 2);
}
- (void)zoomOut {
MKCoordinateRegion region = [self.mapView region];
region.span.latitudeDelta = region.span.latitudeDelta*4;
region.span.longitudeDelta = region.span.longitudeDelta*4;
region.center.latitude = self.mapView.centerCoordinate.latitude;
region.center.longitude = self.mapView.centerCoordinate.longitude;
// The region upper latitude must not exceed 90.0 degrees, and the region lower latitude must not fall below -90.0.
double upperLatitude = region.center.latitude + region.span.latitudeDelta / 2.0;
double lowerLatitude = region.center.latitude - region.span.latitudeDelta / 2.0;
if ( upperLatitude > 90 || lowerLatitude < -90 ) {
region.center.latitude = 0.0;
double spanRatio = region.span.latitudeDelta / region.span.longitudeDelta;
region.span.latitudeDelta = 180;
region.span.longitudeDelta = 180 / spanRatio;
}
[self.mapView setRegion:region animated:YES];
//NSLog(@"zoomOut: center %lf, %lf; spanDelta %lf, %lf, upper left %lf, %lf; lower right %lf %lf", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, region.center.latitude + region.span.latitudeDelta / 2, region.center.longitude - region.span.longitudeDelta / 2, region.center.latitude - region.span.latitudeDelta / 2, region.center.longitude + region.span.longitudeDelta / 2);
}