Mene
Mene

Reputation: 3799

Prevent world-wrapping in GMap v3

I'm currently migrating from v2 to v3. The world should not be repeated longitudinally.

In v2 this could be archived with something like this:

var proj = new GMercatorProjection(30); 
proj.tileCheckRange = function(a,b,c) { 
  var tileBounds = Math.pow(2,b);
  if (a.y<0 || a.y >= tileBounds) {return false;}
  if (a.x<0 || a.x >= tileBounds) {return false;}
  return true; 
};

proj.getWrapWidth = function(zoom) {
  return 99999999999999;
};

G_NORMAL_MAP.getProjection = function() {return proj;};

But I have yet to find a solution for v3.

EDIT To clarify a bit: I'm not looking for a way to prevent panning (navigating sideways) but a way to prevent the map from repeating it self, esp. at low zoom-levels

Upvotes: 6

Views: 3495

Answers (3)

PhistucK
PhistucK

Reputation: 2546

The restriction MapOption property can help here.

new google.maps.Map(
 container,
 {
  restriction:
   {
    latLngBounds: {north: 85, south: -85, west: -179.5, east: 179.5},
    strictBounds: true
   }
 });

This will also take care of the "padding" that is usually displayed beyond the north/east bounds and make the map "end" where the map tiles end.

You can fiddle with the numbers a bit to account for a bit more (or a bit more accurate) area, but I think it should be enough for most cases.

Upvotes: 1

Trott
Trott

Reputation: 70095

Check out the two answers at How do I limit panning in Google maps API V3?. The technique outlined there should get you (depending on your use case) most of the way there or maybe all the way there.

Those answers do not show how to limit wrapping, but they do show how to limit panning. If you can take other measures to restrict what's in the initial viewport (say, if you have control over the size and can restrict the zoom levels and initial coordinates appropriately), then limiting panning can get you there.

Upvotes: 2

spolu
spolu

Reputation: 660

World wrapping can be easily prevented this way (adapted from the answers linked by Trott)

// prevent wrap                                                                                                                                                                                                                                                     
  var lastValid = map.getCenter();
  google.maps.event.addListener(map, 'center_changed', function() {
    if(map.getBounds().getNorthEast().lng() > map.getBounds().getSouthWest().lng()) {
      lastValid = map.getCenter();
    }
    else
      map.panTo(lastValid);
  });

Upvotes: -1

Related Questions