我有一个有多个多行的地图,当点击这条线时,我想打开一个特定于行的信息。
到目前为止,当单击这一行时,我的代码无法显示任何信息,下面是我的代码:
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);
});
}发布于 2014-04-19 16:29:12
没有必要为每个点调用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的content和position,必须为此使用方法:
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的例子。
https://stackoverflow.com/questions/23149326
复制相似问题