【问题标题】:Detect zoom on GMSMapView (iOS)检测 GMSMapView (iOS) 上的缩放
【发布时间】:2016-01-13 18:45:00
【问题描述】:

如何查看用户是否放大/缩小了我的 mapView (GMSMapView)?

我想这样做,如果缩放大于 14,地图上的所有标记都会消失,如果缩放更小,所有标记都会重新加载。

我该怎么做?

谢谢!

【问题讨论】:

    标签: ios objective-c iphone cocoa-touch google-maps-sdk-ios


    【解决方案1】:

    下面的代码假设视图控制器有一个属性“markerArray”,它保存了所有的标记。

    -(void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition*)position {
        int zoom= mapView.camera.zoom;
        if (zoom < 14) {
            [mapView_ clear];
        }
        else{
            [self displayAllMarkers];
        }
    }
    
    
    -(void) displayAllMarkers{
        for (BarObject *barMarker in markerArray){
            GMSMarker *marker = [[GMSMarker alloc] init];
            marker.position = barMarker.location; //
            marker.icon = [GMSMarker markerImageWithColor:[UIColor blackColor]];
            marker.map = mapView_;
        }
     }
    

    【讨论】:

    • 更新了答案。我在我的构建中对其进行了测试,并且可以正常工作。如果您有任何问题,请告诉我。
    【解决方案2】:

    我在夏天也搜索过这个答案,这就是我发现并最终做的:

    在 MKMapView 上创建一个类别: 我把它命名为MKMapView+ZoomLevel

    .h

    //  MKMapView+ZoomLevel.h
    #import <MapKit/MapKit.h>
    @interface MKMapView (ZoomLevel)
    - (int)currentZoomLevel;
    @end
    

    .m

    //  MKMapView+ZoomLevel.m
    #import "MKMapView+ZoomLevel.h"
    #define MERCATOR_OFFSET 268435456
    #define MERCATOR_RADIUS 85445659.44705395
    #define MAX_GOOGLE_LEVELS 20
    @implementation MKMapView (ZoomLevel)
    - (int)currentZoomLevel
    {
        CLLocationDegrees longitudeDelta = self.region.span.longitudeDelta;
        CGFloat mapWidthInPixels = self.bounds.size.width*2;//2 is for retina display
        double zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapWidthInPixels);
        double zoomer = MAX_GOOGLE_LEVELS - log2( zoomScale );
        if ( zoomer < 0 ) zoomer = 0;
        zoomer = round(zoomer);
        return (int)zoomer;
    }
    @end
    

    然后在你的视图控制器中:

    #import "MKMapView+ZoomLevel.h
    
    // insert logic here
    double zoomLevel = [self.mapView currentZoomLevel];
    if (zoomLevel > 14 ) {
      // add code to hide or resize your annotations
        for (id<MKAnnotation> annotation in self.mapView.annotations) {
            if (![annotation isKindOfClass:[MKUserLocation class]]) {
                 [self.mapView removeAnnotation:annotation];
            }
        }
    } 
    

    【讨论】:

    • 我需要 Google Maps SDK for iOS (GMSMapVIew) 而不是 mapKit (MKMapView)
    • 抱歉,误读了问题。我为 google sdk 添加了一个新答案。它应该让你朝着正确的方向前进。回家后,我可以添加更多示例代码或将您链接到我的项目。
    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    相关资源
    最近更新 更多