Prevent scrolling outside map area on google map

て烟熏妆下的殇ゞ 提交于 2020-01-17 04:18:23

问题


Am actually dealing with the Google Maps Framework for iOS, and I want to block scrolling out side a giving area.

What I tried to do at first, implement the delegate method : - (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position and compare the map position with the right/left corners.But what I did has many issues when scrolling or zooming.

Below an exemple of my implementation :

 - (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {  

    if (position.target.latitude > topLat) { 
        GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:topLat
                                                                      longitude:position.target.longitude
                                                                            zoom:position.zoom
                                                                            bearing:220
                                                                            viewingAngle:0];

        [self.mapView animateToCameraPosition:goBackCamera];
    }

    if (position.target.latitude < bottomLat) {
        GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:bottomLat
                                                                      longitude:position.target.longitude
                                                                           zoom:position.zoom
                                                                        bearing:220
                                                                   viewingAngle:0];
        [self.mapView animateToCameraPosition:goBackCamera];
    }

    if (position.target.longitude > rightLong) {
        GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:position.target.latitude
                                                                      longitude:rightLong
                                                                           zoom:position.zoom
                                                                        bearing:220
                                                                   viewingAngle:0];
        [self.mapView animateToCameraPosition:goBackCamera];
    }

    if (position.target.longitude < leftLong) {
        GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:position.target.latitude
                                                                      longitude:leftLong
                                                                           zoom:position.zoom
                                                                        bearing:220
                                                                   viewingAngle:0];
        [self.mapView animateToCameraPosition:goBackCamera];
    }
}

Do you no a way more efficient to deal with this?

PS: TopLat, RightLong ... mean top Latitude and Right longitude etc

Regards


回答1:


One way I can think of is by setting up the bound in the map in you want maps to scroll using GMSCoordinateBounds.

Next thing you can do is to set up the scroll gesture to true just within that section of maps you have specified above bounds. For the rest of the map the scroll gesture should be false.




回答2:


Use GMSCoordinateBounds(northeast co-ordinates, southwest co-ordinates) method. This might be helpful GMSCoordinateBounds Class Reference



来源:https://stackoverflow.com/questions/29120745/prevent-scrolling-outside-map-area-on-google-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!