我将上面 @samshull 的答案(正式投票!)与来自的信息结合起来here https://developers.google.com/maps/documentation/javascript/events?csw=1#EventArguments使信息窗口 https://developers.google.com/maps/documentation/javascript/reference?csw=1#InfoWindow出现在用户光标悬停在该行上方的位置:
// Open the InfoWindow on mouseover:
google.maps.event.addListener(line, 'mouseover', function(e) {
infoWindow.setPosition(e.latLng);
infoWindow.setContent("You are at " + e.latLng);
infoWindow.open(map);
});
// Close the InfoWindow on mouseout:
google.maps.event.addListener(line, 'mouseout', function() {
infoWindow.close();
});
Here, line
是你的PolyLine https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polyline目的;map
是你的Map https://developers.google.com/maps/documentation/javascript/reference?csw=1#Map目的;和infoWindow
是您的 InfoWindow 对象,我刚刚使用它创建:
var infoWindow = new google.maps.InfoWindow();
我也关注这个建议 https://developers.google.com/maps/documentation/javascript/infowindows#add通过为所有折线重新使用相同的 InfoWindow 对象,而不是为每一行创建一个新对象:
最佳实践:为了获得最佳用户体验,只有一个信息窗口
应该随时在地图上打开。多个信息窗口使
地图显得杂乱。如果您一次只需要一个信息窗口,
您可以只创建一个 InfoWindow 对象并在不同的位置打开它
地图事件(例如用户点击)上的位置或标记。如果你这样做
需要多个InfoWindow,可以显示多个InfoWindow
同时对象。
注意infoWindow.setContent()
需要一个字符串。所以打电话toString()
如果您想在信息窗口中显示数字,请在数字变量上。
我认为所有这一切都是一个不完美的解决方法,直到谷歌地图希望有一天添加一个title
财产给折线选项 https://developers.google.com/maps/documentation/javascript/reference#PolylineOptions,就像他们已经做过的那样标记选项 https://developers.google.com/maps/documentation/javascript/reference#MarkerOptions.