检测 GMSMapView 的缩放 (iOS)
Detect zoom on GMSMapView (iOS)
我如何才能知道用户是否在我的 mapView (GMSMapView) 上缩放 in/out?
我想这样做,如果缩放比例大于 14,地图上的所有标记都将消失,如果缩放比例小于 14,则所有标记都将重新加载。
我该怎么做?
谢谢!
整个夏天我也在搜索这个答案,这是我找到并最终做的事情:
在 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];
}
}
}
下面的代码假定视图控制器有一个 属性 的 "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_;
}
}
我如何才能知道用户是否在我的 mapView (GMSMapView) 上缩放 in/out?
我想这样做,如果缩放比例大于 14,地图上的所有标记都将消失,如果缩放比例小于 14,则所有标记都将重新加载。
我该怎么做?
谢谢!
整个夏天我也在搜索这个答案,这是我找到并最终做的事情:
在 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];
}
}
}
下面的代码假定视图控制器有一个 属性 的 "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_;
}
}