Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth Scrolling on Google Maps SDK for iOS

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!

like image 315
shadowarcher Avatar asked May 19 '15 12:05

shadowarcher


People also ask

Is Google Maps iOS SDK free?

The Maps SDK for iOS uses a pay-as-you-go pricing model.

How do I scroll a map on Google Maps?

Move around the map: Use the arrow keys. Tip: To move the map by one square, hold Shift and press the arrow keys.

Is Google Maps SDK free?

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).


1 Answers

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]];
    }
}
like image 190
Danil Avatar answered Oct 02 '22 15:10

Danil