Google Maps API V3:fromContainerPixelToLatLng [英] Google Maps API V3: fromContainerPixelToLatLng
问题描述
我目前正在开发一个项目,要求我在Google Map上方放置一个div。不过,我需要将div的 mousemove
事件传递给Map。为此,我需要从地图容器像素坐标中找到LatLng坐标(因为触发地图 mousemove
事件需要LatLng坐标)。
是否有任何其他方式将div移动到地图上,如果不是,我如何从Map容器坐标转到LatLng。我读过这样做需要创建一个哑覆盖图,然后使用 getProjection()
来获取 MapCanvasProjection
,最后从ContainerPixelToLatLng()调用。有没有更简单的方法,或者我真的必须先创建一个虚拟覆盖?
据我所知,这是你必须这样做的方式。起初我也很不情愿,因为它看起来像这样的矫枉过正,但是一旦我做到了,一切都很好。这里有一个简单的 delayedInit()
回调的示例实现: 函数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(){
// Overlay dummy已准备就绪,可以在
之后调用delayedInit();
}
var dum;
...并且在您实例化Google地图后:
dum = new Dummy(map);
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?
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);
这篇关于Google Maps API V3:fromContainerPixelToLatLng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!