检测GMSMapView(iOS)上的缩放

时间:2016-01-13 18:45:01

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

如何找到用户放大/缩小我的mapView(GMSMapView)?

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

我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:0)

我在夏天也搜索了这个答案,这是我发现并最终完成的事情:

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

答案 1 :(得分:0)

以下代码假设视图控制器具有&#34; markerArray&#34;的属性。它包含所有标记。

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