iOS:放大和缩小地图上的效果

时间:2012-11-14 14:44:24

标签: ios objective-c ios5 mapkit zoom

我已经在this link创建了地图,并且效果很好。

但问题是,它只能放大一种方式(它只会变大)。我怎么能让它在任何一种方式上工作?

可能就像我们在google map上一样(加上 - 减去左侧的棍子)。

2 个答案:

答案 0 :(得分:3)

对于ZoomIn

-(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];
}

适用于ZoomOut

-(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];
}
  

您可以使用此结构中的delta值来指示所需的值   缩放地图的级别,较小的delta值对应于a   更高的缩放级别。

请参阅此link了解更多信息。

答案 1 :(得分:0)

这是Midhun副总裁和Slavco Petkovski对答案的修订,该答案避免了对于"无效区域"的回复的NSInvalidArgumentException。在zoomOut中设置latitudeDelta太大。放大并没有任何问题 - 一旦达到极限,它就不会放大。

- (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);
}