IOS – detection GMSMapView zoom

Is there a way to detect zoom (pinch and double click) in this Google Map Services component?

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture

The above method will cause fire regardless of the action.

There is another way to detect when the zoom (or any other attributes) has changed – Key-value observation (aka KVO). It is especially useful when there is no delegate method provided for us. From Apple docs:

Key-value observing provides a mechanism that allows objects to be
notified of changes to specific properties of other objects.

No matter where you set up the map view, please add the following code snippet:

p>

[self.mapView addObserver:self forKeyPath:@"camera.zoom" options:0 context:nil];

Now you only need to implement -observeValueForKeyPath: ofObject: change: context: the method to actually receive the callback. Like this:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

if ([keyPath isEqualToString:@"camera.zoom"]) {

// this static variable will hold the last value between invocations.
static CGFloat lastZoom = 0;

GMS MapView *mapView = (GMSMapView *)object;
CGFloat currentZoom = [[mapView camera] zoom];

if (!(fabs((lastZoom)-(currentZoom))
//Zoom level has actually changed!
NSLog(@"Zoom changed to: %.2f", [[mapView camera] zoom]);

}

//update last zoom level value.
lastZoom = currentZoom;

}
}

Don’t forget Delete the observer in -dealloc or -viewDidDissapear according to your needs:

- (void)dealloc {

[self.mapView removeObserver:self forKeyPath:@"camera.zoom"];

}

Happy coding:-)

Is there any way Detect zoom (pinch and double click) in this Google Map Services component?

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture

The above method will cause fire regardless of the action.

There is another way to detect when the zoom (or any other property) has changed-Key Value Observation (aka KVO). When there is no It is particularly useful when we provide the delegate method. From Apple docs:

Key-value observing provides a mechanism that allows objects to be
notified of changes to specific properties of other objects.

No matter where you set up the map view, please add the following code snippet:

 [self.mapView addObserver:self forKeyPath:@"camera.zoom" options:0 context:nil];

Now you only need to implement -observeValueForKeyPath:ofObject:change:context: the method that actually receives the callback. Like this:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {< br />
if ([keyPath isEqualToString:@"camera.zoom"]) {

// this static variable will hold the last value between invocations.
static CGFloat lastZoom = 0;

GMSMapView *mapView = (GMSMapView *)object;
CGFloat current Zoom = [[mapView camera] zoom];

if (!(fabs((lastZoom)-(currentZoom))
//Zoom level has actually changed!
NSLog(@"Zoom changed to: %.2f", [[mapView camera] zoom]);

}

//update last zoom level value.
lastZoom = currentZoom;

}
}

Don’t forget to delete the observers in -dealloc or -viewDidDissapear according to your needs:

- (void)dealloc {

[self.mapView removeObserver:self forKeyPath:@"camera.zoom"];
< br />}

Happy coding:-)

Leave a Comment

Your email address will not be published.