Question

Using google maps v3 javascript API i cannot find a way to block the panning of the map over the north pole or under the south pole. As an example embedded maps on this page: https://developers.google.com/maps/documentation/embed/guide has the same behaviour, zooming out with the world view and panning north bring the view in a complete gray area.

How to prevent like it's done on official site http://maps.google.it ?

Was it helpful?

Solution 2

Working for me with the attribute "restriction":

new google.maps.Map(document.getElementById('#mapCanvas'), {
  zoom: 2,
  center: {lat: 0, lng: 0},
  tilt: 0,
  restriction: {
    latLngBounds: {north: 85, south: -85, west: -180, east: 180},
    strictBounds: true
  },
});

OTHER TIPS

UPDATE - The following answer doesn't look to work anymore, I suppose it's because google maps API has been upgrade. I leave the code here for reference.

Thanks to geocodezip comments i modified Mike Williams' solution for my case.

Here is the fiddle example

Relevant code part:

google.maps.event.addListener(map, 'center_changed', function() {
    checkBounds(map);
});
// If the map position is out of range, move it back
function checkBounds(map) {

var latNorth = map.getBounds().getNorthEast().lat();
var latSouth = map.getBounds().getSouthWest().lat();
var newLat;

if(latNorth<85 && latSouth>-85)     /* in both side -> it's ok */
    return;
else {
    if(latNorth>85 && latSouth<-85)   /* out both side -> it's ok */
        return;
    else {
        if(latNorth>85)   
            newLat =  map.getCenter().lat() - (latNorth-85);   /* too north, centering */
        if(latSouth<-85) 
            newLat =  map.getCenter().lat() - (latSouth+85);   /* too south, centering */
    }   
}
if(newLat) {
    var newCenter= new google.maps.LatLng( newLat ,map.getCenter().lng() );
    map.setCenter(newCenter);
    }   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top