How to show tooltip on each google map marker
本问题已经有最佳答案,请猛点这里访问。
我在每个谷歌地图标记上显示工具提示时遇到一些问题。
但它只显示一个标记上的工具提示。
这是我的小提琴
http://jsfiddle.net/amit4mins/6UnTr/35/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | $(document).ready(function(){ var locations=[ ['loc1',40.23, -70.34], ['loc7',40.23, -70.34], ['loc8',40.23, -70.34], ['loc2',41.24, -71.35], ['loc3',42.25, -72.36], ['loc4',43.26, -73.37], ['loc5',44.27, -74.34], ['loc6',45.28, -75.34], ['loc6',46.29, -76.34]]; function initialize() { console.log(locations); var mapOptions = { center: new google.maps.LatLng(-34.397, 150.644), zoom: 3, mapTypeId: google.maps.MapTypeId.ROADMAP, }; var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions); var marker, i; for (i = 0; i < locations.length; i++) { var infoWindow = new google.maps.InfoWindow({ content: locations[i][0], }); marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map, title:locations[i][0] }); google.maps.event.addListener(marker, 'click', function() { infoWindow.open(map, marker); }); } marker.setMap(map); } google.maps.event.addDomListener(window, 'load', initialize); }); |
谁能让我知道我做错了什么。
非常感谢,
M.
更改您的添加侦听器代码,如下所示
1 2 3 4 5 6 | google.maps.event.addListener(marker, 'click', (function(mm, tt) { return function() { infoWindow.setContent(tt); infoWindow.open(map, mm); } })(marker, locations[i][0])); |
DEMO