javascript - Google map v3 折线工具提示

标签 javascript google-maps google-maps-api-3

谷歌地图标记对象 (google.maps.Marker) 有一个标题属性,因此当用户将鼠标移到标记上时,会显示一个简单的工具提示。

多段线 (google.maps.Polyline) 上没有标题属性。有什么办法可以在 V3 中做到这一点/模拟这一点吗? 我可以在 V2 中执行此操作,但我找不到 V3 的示例。

最佳答案

我将上面@samshull 的回答(正式投票!)与来自 here 的信息相结合制作 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();
});

在这里,line 是您的 PolyLine目的; map 是你的 Map目的; infoWindow 是您的 InfoWindow 对象,我刚刚创建它:

var infoWindow = new google.maps.InfoWindow();

我也关注this advice通过为我的所有折线重新使用相同的 InfoWindow 对象,而不是为每条线创建一个新对象:

Best practices: For the best user experience, only one info window should be open on the map at any one time. Multiple info windows make the map appear cluttered. If you only need one info window at a time, you can create just one InfoWindow object and open it at different locations or markers upon map events, such as user clicks. If you do need more than one info window, you can display multiple InfoWindow objects at the same time.

请注意,infoWindow.setContent() 接受一个字符串。因此,如果您想在信息窗口中显示数字,请对数字变量调用 toString()

我认为所有这些都是一个不完美的解决方法,直到谷歌地图希望有一天将 title 属性添加到 PolylineOptions ,就像他们已经为 MarkerOptions 所做的那样.

关于javascript - Google map v3 折线工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5112867/

相关文章:

javascript - 在小屏幕上固定定位的 html div 有什么好的解决方案?

javascript - 谷歌地图搜索框

javascript - Google map 和 Fusion 表 - 加载错误

javascript - 如何为 Google 街景实现 "move forward"操作?

javascript - Google Maps api v3 - 在从点击事件中添加新标记之前删除标记

javascript - Internet Explorer 中的 ACE 代码编辑器问题

javascript - 无法读取 dojo 中未定义的属性 'set'

javascript - 如何在 angular.js 中使用 $http 创建同步?

google-maps - 谷歌地图 API V3 : KML plus additional marker not zooming to fit both

google-maps - 如何自定义谷歌地图以摆脱 3D 建筑效果?