Pan point on Google Map to specific pixel position on screen (API v3)

痴心易碎 提交于 2019-12-21 02:53:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2777844/pan-point-on-google-map-to-specific-pixel-position-on-screen-api-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!