【问题标题】:Multiple polylines and infowindows with Google Maps V3使用 Google Maps V3 的多个折线和信息窗口
【发布时间】:2011-05-03 08:36:09
【问题描述】:

我有一张包含多条折线的地图,并希望在单击该线时打开一个特定于线的信息窗口。

到目前为止,我的代码只显示了最后一次迭代的内容。

我找到了两个很好的例子,但经过几个小时的尝试,我仍然没有进一步的想法。
示例 1:http://srsz750.appspot.com/api3/polylines-multiple.html
示例 2:http://www.geocodezip.com/v3_GenericMapBrowser.asp?filename=flights090414.xml

所以你是我的最后一枪:-S

这是我的代码,只显示了最后一次迭代的内容:

for (var i = 0; i < locations.length; i++) {
var route = locations[i]; // locations is an array of route-arrays.   
 //route is an array with details. route[0] contains the description.

var imageStart = 'img/rijder.png';   
var polyOptions = {
     strokeColor: '#0000FF',
         strokeOpacity: 1.0,
     strokeWeight: 3
    }       

  poly = new google.maps.Polyline(polyOptions, info);   
  var path = poly.getPath(); 

 //line text

 var info = route[0]; 
 google.maps.event.addListener(poly, 'click', function(event) {
    infowindow.setContent(info);
    infowindow.position = event.latLng;
infowindow.open(map);
    }); 

//startmarker   
var startLatLng = new google.maps.LatLng(route[1], route[2]);
var smarker = new google.maps.Marker({
    position: startLatLng,
    map: map,
    icon: imageStart,
    title: route[7],
    html: route[0]     
});
path.push(startLatLng);
//starttext

 google.maps.event.addListener(smarker, "click", function () {
        infowindow.setContent(this.html);
        infowindow.open(map, this);
        }); 

 //endmarker
var endLatLng = new google.maps.LatLng(route[4], route[5]);
var emarker = new google.maps.Marker({
    position: endLatLng,
    map: map,
    icon: imageEnd,
    title: route[8],
    html: route[3]     
});
path.push(endLatLng);
//endtext

 google.maps.event.addListener(emarker, "click", function () {
            infowindow.setContent(this.html);
            infowindow.open(map, this);
        });
//close line and put on the map
poly.setMap(map); 
 } 
}

这是我希望工作的代码,但所有行都消失了(只有相关代码,我还添加了鼠标悬停功能):

  //line text
     google.maps.event.addListener(poly, 'click', (function(event,index){
     return function(){
    var routeinfw = locations[index];
    var inf =  routeinfw[0]; 
    infowindow.setContent(inf);
    infowindow.position = mouselocation;
    infowindow.open(map);
      };
    })(event,i));

//starticon

【问题讨论】:

    标签: google-maps google-maps-api-3 infowindow polyline


    【解决方案1】:

    就像您发布的示例一样,创建一个函数来创建点击事件侦听器并从位置循环内部调用它:

    function createInfoWindow(poly,content) {
        google.maps.event.addListener(poly, 'click', function(event) {
            infowindow.content = content;
            infowindow.position = event.latLng;
            infowindow.open(map);
        });
    }
    

    你可以在循环结束时调用它:

    createInfoWindow(poly,info);
    

    【讨论】:

    • 您的代码解决了多边形的锚定问题(它们不能很好地锚定 event.latLng - 标记,好的)。因此,明确定位 infowindow,而不是“infowindow.open(map, event.latLng)”,是解决谷歌地图错误多边形事件界面的好方法。
    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多