Sunday, 31 May 2015

googlemap bounds controll



To control google map boundary, you should use google.maps.LatLngBounds After map pans(users drag and drop to move map) , we should verify if new map center is still in the bound.

Below link shows world map boundary:

http://stackoverflow.com/questions/11849636/maximum-lat-and-long-bounds-for-the-world-google-maps-api-latlngbounds

var strictBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(85, -180),           // top left corner of map
    new google.maps.LatLng(-85, 180)            // bottom right corner
);

However if you use this bounds to verify it's center, like this:

    console.log(allowedBounds.contains(allowedBounds.getCenter()));

You will get "false".

Blow blog also mentioned this issue:

http://stackoverflow.com/questions/5405539/google-maps-v3-why-is-latlngbounds-contains-returning-false

I finally realise that,  the constructor of LatLngBounds() use south-west and north-east corners:

https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds















No comments:

Post a comment