Google Maps API V3: fromContainerPixelToLatLng

匿名 (未验证) 提交于 2019-12-03 02:22:01

问题:

I'm currently working on a project that requires that I have a div stacked above a Google Map. However, I need to pass the mousemove event of the div to the Map. To do that, I need to find the LatLng co-ordinates from the map container pixel co-ordinate (since triggering the Maps mousemove event requires the LatLng co-ordinates).

Is there any other way to pass the mousemove event from the div to the map, and if not, how do I go from the Map container co-ordinates to LatLng. I read that doing so requires creating a dummy overlay, and then using the getProjection() on that to get a MapCanvasProjection, and finally calling the fromContainerPixelToLatLng(). Is there any simpler way or do I really have to create a dummy overlay first?

回答1:

As far as I can tell, this is the way you have to do it. I was reluctant at first, too, since it seemed like such overkill, but once I did it everything worked great. Here's an example implementation with a convenient delayedInit() callback:

function Dummy(map) {     this.setMap(map); } Dummy.prototype = new google.maps.OverlayView(); Dummy.prototype.draw = function() {     if (!this.ready) {          this.ready = true;          google.maps.event.trigger(this, 'ready');      }  } Dummy.prototype.onAdd = function(){     // the Overlay dummy is ready and can be called upon     delayedInit(); } var dum; 

... and after you've instantiated your Google map:

dum = new Dummy(map); 


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