Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 291
Mene Avatar asked Apr 12 '11 11:04

Mene


2 Answers

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.

like image 56
Trott Avatar answered Oct 26 '22 09:10

Trott


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.

like image 1
PhistucK Avatar answered Oct 26 '22 08:10

PhistucK