【问题标题】:Google Maps API V3: fromContainerPixelToLatLngGoogle Maps API V3:fromContainerPixelToLatLng
【发布时间】:2010-09-05 01:08:23
【问题描述】:

我目前正在开展一个项目,该项目需要我在 Google 地图上方堆叠一个 div。但是,我需要将 div 的 mousemove 事件传递给 Map。为此,我需要从地图容器像素坐标中找到 LatLng 坐标(因为触发 Maps mousemove 事件需要 LatLng 坐标)。

有没有其他方法可以将鼠标移动事件从 div 传递到地图,如果没有,我如何从地图容器坐标转到 LatLng。我读到这样做需要创建一个虚拟覆盖,然后在上面使用getProjection() 来获得MapCanvasProjection,最后调用fromContainerPixelToLatLng()。有没有更简单的方法,还是我真的必须先创建一个虚拟叠加层?

【问题讨论】:

标签: google-maps-api-3


【解决方案1】:

据我所知,这是您必须这样做的方式。一开始我也很不情愿,因为这似乎太过分了,但一旦我这样做了,一切都很好。这是一个带有方便的delayedInit() 回调的示例实现:

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;

...在您实例化您的 Google 地图之后:

dum = new Dummy(map);

【讨论】:

  • 谢谢。实验后我在同一个地方结束。虽然,我只是定义了 draw、onAdd 和 onRemove 函数并将它们留空。像魅力一样工作!谢谢。
猜你喜欢
  • 2023-04-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 2012-06-10
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多