谷歌地图API从V2转换为V3

时间:2013-04-25 15:02:41

标签: javascript google-maps google-maps-api-3 google-maps-api-2

我正在努力将以下2个google m aps V2 API事件转换为V3,但不能为我的生活弄清楚如何做到这一点,有人可以给我一些指示吗?

    GEvent.addListener(map, "click", function(overlay, point){
    if (point != null) {
      window.status = "Click " + point.lat() + ", "+ point.lng();
    } 
          else if (overlay != null) 
        {
      window.status = "Overlay " + lastPoint.lat() + ", "+ lastPoint.lng();
    }
});
GEvent.addListener(map, "mousemove", function(point){
 lastPoint = point;
});

2 个答案:

答案 0 :(得分:1)

试试这个:

google.maps.event.addListener(map, 'click', function(event) {
  console.log(event.latLng);
});

“v3事件侦听器中不存在重叠参数。如果在v3映射上注册click事件,则只有在用户单击基本映射时才会发生回调。如果您在可点击覆盖上注册其他回调,则可以需要对这些点击做出反应。“ - https://developers.google.com/maps/articles/v2tov3#events

答案 1 :(得分:0)

如果您需要点击叠加层并让叠加层可点击,请从叠加层传播点击:

var map = new google.maps.Map(document.getElementById("mapcanvas"), mapOptions);

google.maps.event.addListener(map, 'click', function(event) {
    console.log(event.latLng);
});

google.maps.event.addListener(overlay, 'click', function(event) {
    google.maps.event.trigger(map, 'click', event);
});