Pan point on Google Map to specific pixel position on screen (API v3)
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 开发者_C百科have the methods I need.
Is there a way to do this without making an overlay specificaly for it?
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.
精彩评论