使用Google地图的折线和信息窗口?

时间:2014-04-18 07:18:01

标签: google-maps infowindow

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

到目前为止,我的代码在点击该行时无法显示任何信息窗口, 这是我的代码:

 var poly;
        var polyOptions = {
          strokeColor: '#ff0000',    
          strokeOpacity: 1.0,   
          strokeWeight: 3    
        }
        poly = new google.maps.Polyline(polyOptions);
        poly.setMap(map);   

        for(var i=0; i<locations.length; i++){
            var loc = locations[i].split(',');
            var path = poly.getPath();    
            path.push(new google.maps.LatLng(loc[0], loc[1]));    
            createInfoWindow(poly,'polyinfo...test');
        }


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

1 个答案:

答案 0 :(得分:3)

无需为每个点拨打poly.getPath()createInfoWindow(poly, 'polyinfo...test');。它只能在创建整个路径之前和之后调用一次:

var path = poly.getPath();

for (var i = 0; i < locations.length; i++) {
    //var loc = locations[i].split(',');
    var loc = locations[i];
    path.push(new google.maps.LatLng(loc[0], loc[1]));
    //createInfoWindow(poly, 'polyinfo...test');
}

createInfoWindow(poly, 'polyinfo...test');

要设置infowindow的contentposition,您必须使用以下方法:

function createInfoWindow(poly, content) {

    google.maps.event.addListener(poly, 'click', function(event) {
        // infowindow.content = content;
        infowindow.setContent(content);

        // infowindow.position = event.latLng;
        infowindow.setPosition(event.latLng);
        infowindow.open(map);
    });
}

查看整个example at jsbin