【问题标题】:iOS : zoom in and zoom out effect on mapiOS:地图上的放大和缩小效果
【发布时间】:2012-11-03 01:55:12
【问题描述】:

我已经创建了this link 上显示的地图,并且它运行良好。

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

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

【问题讨论】:

    标签: ios objective-c ios5 mapkit zooming


    【解决方案1】:

    这是 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);
    }
    

    【讨论】:

    • 验证来自我们,答案不会包括 A-Z 的所有内容。
    【解决方案2】:

    放大

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

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

    更多信息请参考link

    【讨论】:

    • 感谢您的回答。如有任何疑问,我会调查并回复您。
    • 我添加了这段代码并将区域变量设为全局变量MapViewController { MKCoordinateRegion region; },并将MKCoordinateRegion region = {cords,span}; 更改为region = {cords,span};。但我在region = {cords,span};Expected expression 发出红色警告。我错过了什么?
    • 同样在region.span.latitudeDelta 得到错误Member reference type 'MKCoordinateRegion *' is a pointer; maybe you meant to use '-&gt;'?
    • @FahimParkar:您将区域声明为MKCoordinateRegion region;,这就是问题所在。声明为MKCoordinateRegion *region;
    • 我把它改成了region = (MKCoordinateRegion) {cords,span};,它工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多