首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌地图的折线和信息流?

谷歌地图的折线和信息流?
EN

Stack Overflow用户
提问于 2014-04-18 07:18:00
回答 1查看 5.5K关注 0票数 0

我有一个有多个多行的地图,当点击这条线时,我想打开一个特定于行的信息。

到目前为止,当单击这一行时,我的代码无法显示任何信息,下面是我的代码:

代码语言:javascript
复制
 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);
         });
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-19 16:29:12

没有必要为每个点调用poly.getPath()createInfoWindow(poly, 'polyinfo...test');。在创建整个路径之前和创建整个路径时,只能调用一次:

代码语言:javascript
复制
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,必须为此使用方法:

代码语言:javascript
复制
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);
    });
}

见整个jsbin的例子

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23149326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档