0

I wonder how to make a google map not to leave world bounds, like on google maps site itself. I am using javascript google map api v3, here is my map options:

mapOptions = {
  center: new google.maps.LatLng(20, 0),
  zoom: 3,
  minZoom: 3,
  zoomControl: true,

I've restricted map zoom, but user still can drag out of world bounds and even select region there.

Snapshot

How can I prevent this behavior?

zegoline
  • 574
  • 2
  • 8
  • 21
  • You could check the map bounds while dragging, and set the center back to the last acceptable value when the bounds fall off your criteria. – MrUpsidown Apr 14 '15 at 12:43
  • 1
    Have a look at [this answer](http://stackoverflow.com/a/27115480/1238965) btw. – MrUpsidown Apr 14 '15 at 12:45

2 Answers2

1

You have to make a general check using some conditional statement like if...else and comparing if the current Lat/Lng is falling out of a specified boundary (which is a world bound in your case).

I would recommend you to look at this SO question that may provide you some leads of how to proceed.

Community
  • 1
  • 1
AniV
  • 3,997
  • 1
  • 12
  • 17
0

Found a nice solution I'd like to share:

if (!allowedBounds.intersects(map.getBounds())) {
        map.panToBounds(allowedBounds);
     }

it will return to the initial set bounds (minimum way) when the whole screen left the bounds.