I have a some UI elements on the right of my map (sometimes), and I'd like to offset my panTo() calls (sometimes).
So I figured:
- get the original latlng
- convert it to screen pixels
- add an offset
- convert it back to latlng.
But I must misunderstand what Google Maps API refers to as the "Point Plane":
http://code.google.com/apis/maps/documentation/javascript/reference.html#Projection
Here is my code that seems to offset by lat-long:
function getCentreOffset( alatlng ) {
var PIXEL_OFFSET= 100;
var aPoint = me.gmap.getProjection().fromLatLngToPoint(alatlng);
aPoint.x=aPoint.x + OFFSET;
return me.gmap.getProjection().fromPointToLatLng(aPoint);
}
Best Answer
Here's a simpler version of Ashley's solution:
You can then use it like this:
Here is a working example.
Let me explain in detail. This extends the Map object itself. So you can use it just like
panTo()
with extra parameters for offsets. This uses thefromLatLngToContainerPixel()
andfromContainerPixelToLatLng()
methods of the MapCanvasProjecton class. This object has no contructor and has to be gotten from thegetProjection()
method of the OverlayView class; the OverlayView class is used for the creation of custom overlays by implementing its interface, but here we just use it directly. BecausegetProjection()
is only available afteronAdd()
has been called. Thedraw()
method is called afteronAdd()
and is defined for our instance of OverlayView to be a function that does nothing. Not doing so will otherwise cause an error.