I have the following problem. I have google maps api v3 implemented on my site. And the problem is the following. The user can scroll top as much as he wants, meaning, he's getting out of the visible map area.
Example: http://woist.es/
If you pant to the far top you can pan far over greenland and have a grey area. Google maps itself doesn't allow this.
I saw this post here How do I limit panning in Google maps API V3?
and wanted to adjust it to my requirements. Like this
// bounds of the desired area
var allowedBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(85, -180), // top left corner of map
new google.maps.LatLng(-85, 180)
);
var lastValidCenter = map.getCenter();
// Add event listener to remove highlighted POI images
google.maps.event.addListener(map, 'center_changed', function() {
if (allowedBounds.contains(map.getCenter())) {
console.log('valid');
// still within valid bounds, so save the last valid position
lastValidCenter = map.getCenter();
} else {
console.log(map.getCenter());
console.log('not valid');
// not valid anymore => return to last valid position
map.panTo(lastValidCenter);
}
});
However, I always come into the else case. This is an example result of the console.log(map.getCenter());
{
B: 5.00233685
k: 23.3818240
}
How can I limit this correctly?