【问题标题】:Wrong positioning of Google Maps infoWindow谷歌地图信息窗口定位错误
【发布时间】:2015-01-03 15:33:57
【问题描述】:

我有一个位于http://arquitectospelomundo.com 的网站,它显示了几个标记,由markerclusterer 函数组合而成,单击时会显示一个包含一些信息的信息窗口。直接点击地图时,信息窗口出现在正确的位置;但是当单击侧边栏(在其中一张图片上)时,信息窗口会超出范围。 这工作正常,突然改变了;我试图弄清楚,但到目前为止还没有成功。如果有任何帮助指出我正确的方向,我将不胜感激。 谢谢。

【问题讨论】:

标签: google-maps infowindow


【解决方案1】:

问题似乎与 MarkerClusterer 有关。

当触发点击的标记位于集群内时,标记的 map-property 为 null 并且您得到错误的位置。

可能的解决方案:

当标记在集群内时,使用隐藏标记(通过 visible 隐藏)作为 infoWindow 的锚点:

google.maps.event.addListener(marker, 'click', function() {
   //create the dummy-marker on first click
   if(!map.get('dummy')){
     map.set('dummy',new google.maps.Marker({map:map,visible:false}))
   }
   var latLng = marker.getPosition();

   //when the marker is within a cluster
   if(!marker.getMap()){
    //set position of the dummy
    map.get('dummy').setPosition(latLng);
    //use dummy as infowindow-anchor
    marker= map.get('dummy');
   }
    map.panTo(latLng);
    infowindow.setContent(contentString);
    infowindow.open(map,marker);

});

【讨论】:

  • 非常感谢您的解决方案。我不知道为什么会这样,因为它几天前还在工作!但是这段代码解决了这个问题。再次感谢。
【解决方案2】:

我在使用 Marker clusterer 和外部点击地图时遇到了同样的问题,打开 infowindows。 @Dr.Molle 使用虚拟标记的解决方案是一个不错的方向,但问题是您可以使用虚拟进行平移,但听众会触发对不在地图上的标记的点击。正确的信息窗口打开,但自动平移功能无法知道平移到哪里,因此它在最后一次单击后保持在原来的位置。这是从地图外部重复点击的问题。第一次点击正常,但第二次或第三次点击出错,取决于它是否在集群中。

我找到的解决方案测试标记是否在集群中,如果是,则将其附加到地图上,并在单击后将其删除(不确定是否有必要,可能是为了防止地图变慢)。我把它放在每次点击外部调用的函数中。但也许您也可以将其构建到事件侦听器中。这对我来说非常有效:

function openInfoWindow(id){

    googlemap.setZoom(13);
    var marker = markers[id];
    var latLng = marker.getPosition();

    if(!marker.getMap()){ //if the clicked marker is in a cluster, so it is not attached to a map

        marker.setMap(googlemap); //attach it to the map
        google.maps.event.trigger(marker, 'click'); // the standard trigger, opens infowindow 
        googlemap.panTo(latLng); 
        marker.setMap(null); //detach it from the map (not sure if necessary

    } else {

        google.maps.event.trigger(marker, 'click'); 
        googlemap.panTo(latLng);

    }

    return false;
}

【讨论】:

    猜你喜欢
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多