3

When overlay is a Google maps overlay and offsetx, offsety is the pixel distance from the maps center that I want to pan latlong to, the following works.

var projection = overlay.getProjection();
var pxlocation = projection.fromLatLngToContainerPixel(latlong);
map.panTo(projection.fromContainerPixelToLatLng(new google.maps.Point(pxlocation.x+offsetx,pxlocation.y+offsety)));

However, I don't always have an overlay on the map and map.getProjection() returns a projection, not a MapCanvasProjection which does not have the methods I need.

Is there a way to do this without making an overlay specificaly for it?

jeff
  • 8,300
  • 2
  • 31
  • 43
Jake
  • 12,713
  • 18
  • 66
  • 96

1 Answers1

4

Since nobody has come up with a better way, here's my solution. Make an overlay with the sole job of giving me a MapCanvasProjection object.

var helper = new google.maps.OverlayView();
helper.setMap(map);
helper.draw = function () { 
    if (!this.ready) { 
        this.ready = true; 
        google.maps.event.trigger(this, 'ready'); 
    } 
}; 
var projection = helper.getProjection();
...

Hope this helps someone. Better suggestions welcome.

jeff
  • 8,300
  • 2
  • 31
  • 43
Jake
  • 12,713
  • 18
  • 66
  • 96
  • 1
    The google docs says, "The projection is not initialized until onAdd is called by the API." So calling helper.getProjection(); immediately after creating helper, returns null. So use this: helper.onAdd = function(){ projection = helper.getProjection(); }; – nidheeshdas Dec 11 '14 at 09:10