Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit panning in Google maps API V3?

In V2 there was a way to limit panning/dragging so the map stays within certain bounds. How is that done in V3?

Let's say I want the users to only look at Europe. I've already limited the zoom, but if I allow dragging (which I have to in this case, for other reasons) then the user can pan beyond the area I want to show.

Please give working example or code snippet - I'm not an expert coder...

like image 352
E. Tal Avatar asked Jun 26 '10 18:06

E. Tal


People also ask

How do I limit areas on google maps?

Step 1 Go to Add or Edit Map and scroll down to 'Limit Panning Settings' section. Step 2 Enable 'Limit Panning' tab.

Can you customize google maps API?

Your maps can now match your brand and style across your website and your apps! The Google Maps APIs now support you in creating beautiful styled maps for your Android and iOS apps as well as your website using the same JSON style object.

How do I put pans on google maps?

To pan around on a map, you can use the arrow keys to move north, south, east and west. You can also press two arrow keys together to move diagonally.


1 Answers

I guess I'm a little bit late to the party, but since this was exactly what I needed just now AND I improved on it, I thought I'd post an answer anyway.

With both the answers of Daniel Vassallo and brendo, the user can still use the pan-control (if it's activated) to move away from the wanted area. The thing @Yauhen.F mentioned in a comment.

So instead of using the dragend event, I use the center_changed event. This is continuously fired during dragging and every time someone uses the pan control.

// bounds of the desired area var allowedBounds = new google.maps.LatLngBounds(      new google.maps.LatLng(70.33956792419954, 178.01171875),       new google.maps.LatLng(83.86483689701898, -88.033203125) ); var lastValidCenter = map.getCenter();  google.maps.event.addListener(map, 'center_changed', function() {     if (allowedBounds.contains(map.getCenter())) {         // still within valid bounds, so save the last valid position         lastValidCenter = map.getCenter();         return;      }      // not valid anymore => return to last valid position     map.panTo(lastValidCenter); }); 

By saving the last valid position continuously during the dragging, the movement will just stop once it's out of bounds, instead of yerking back once the dragging ended. ......

like image 174
HenningJ Avatar answered Nov 06 '22 15:11

HenningJ