So I want to limit the Google Maps scrollable area to a certain rectangle on the map based off of the latitude and Longitude values. In order to do this I've written the following code:
-(void) viewDidLoad{
startLat = 43.331635f;
startLong = -74.472913f;
endLat = 43.329106f;
endLong = -74.470589f;
float cameraPosLat = (startLat + endLat) / 2.0f;
float cameraPosLong = (startLong + endLong) / 2.0f;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:cameraPosLat
longitude:cameraPosLong
zoom:18];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.mapType = kGMSTypeSatellite;
mapView.delegate = self;
mapView.myLocationEnabled = YES;
[mapView setMinZoom:18 maxZoom:mapView.maxZoom];
self.view = mapView;
marker.map = mapView;
}
-(void) mapView:(GMSMapView *)delegateMapView didChangeCameraPosition:(GMSCameraPosition *)position{
if(delegateMapView.camera.target.latitude > startLat){
[delegateMapView moveCamera:[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(startLat, delegateMapView.camera.target.longitude)]];
}
if(delegateMapView.camera.target.latitude < endLat){
[delegateMapView moveCamera:[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(endLat, delegateMapView.camera.target.longitude)]];
}
if(delegateMapView.camera.target.longitude < startLong){
[delegateMapView moveCamera:[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(delegateMapView.camera.target.latitude, startLong)]];
}
if(delegateMapView.camera.target.longitude > endLong){
[delegateMapView moveCamera:[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(delegateMapView.camera.target.latitude, endLong)]];
}
}
And this works well, it stops the map on the points that I want it to stop at, however one thing I have noticed is that on the edges of the acceptable bounds the scrolling is very jumpy, rather than smooth. I was wondering if there was any way to make sure that the map stayed within the bounds specified, while also maintaining smooth scrolling on the edges.
Any help would be greatly appreciated, thank you!
The Maps SDK for iOS uses a pay-as-you-go pricing model.
Move around the map: Use the arrow keys. Tip: To move the map by one square, hold Shift and press the arrow keys.
Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).
Try this instead:
-(void) mapView:(GMSMapView *)delegateMapView didChangeCameraPosition:(GMSCameraPosition *)position{
if(delegateMapView.camera.target.latitude > startLat){
[delegateMapView animateToCameraPosition:[GMSCameraPosition
cameraWithLatitude:startLat
longitude:delegateMapView.camera.target.longitude
zoom:delegateMapView.camera.zoom]];
}
if(delegateMapView.camera.target.latitude < endLat){
[delegateMapView animateToCameraPosition:[GMSCameraPosition
cameraWithLatitude:endLat
longitude:delegateMapView.camera.target.longitude
zoom:delegateMapView.camera.zoom]];
}
if(delegateMapView.camera.target.longitude < startLong){
[delegateMapView animateToCameraPosition:[GMSCameraPosition
cameraWithLatitude:delegateMapView.camera.target.latitude
longitude:startLong
zoom:delegateMapView.camera.zoom]];
}
if(delegateMapView.camera.target.longitude > endLong){
[delegateMapView animateToCameraPosition:[GMSCameraPosition
cameraWithLatitude:delegateMapView.camera.target.latitude
longitude:endLong
zoom:delegateMapView.camera.zoom]];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With