这是最简单的我可以减少它:
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>Google Maps Multiple Markers</title> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> </head> <body> <div id="map" style="width: 500px; height: 400px;"></div> <script type="text/javascript"> var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; var map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: new google.maps.LatLng(-33.92, 151.25), mapTypeId: google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(locations[i][0]); infowindow.open(map, marker); } })(marker, i)); } </script> </body> </html>
截图:
将回调参数传递给时,会发生一些关闭魔法 addListener 方法。如果您不熟悉闭包的工作方式,这可能是一个非常棘手的主题。如果是这样的话,我建议您查看以下Mozilla文章以获得简要介绍:
addListener
来自 Daniel Vassallo的回答 ,这是一个以更简单的方式处理闭包问题的版本。
因为所有标记都有个体 信息窗口 并且由于JavaScript不关心您是否向对象添加额外属性,因此您只需添加一个 信息窗口 到了 标记的 属性然后调用 .open() 在...上 信息窗口 从它自己!
.open()
的 编辑: 强> 有足够的数据,页面加载可能会花费很多时间,所以而不是构建 信息窗口 使用标记,施工应仅在需要时进行。注意用于构造的任何数据 信息窗口 必须附加到 标记 作为财产( data )。另请注意,在第一次单击事件后, infoWindow 将作为其标记的属性保留,因此浏览器不需要不断重建。
data
infoWindow
var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; var map = new google.maps.Map(document.getElementById('map'), { center: new google.maps.LatLng(-33.92, 151.25) }); for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map, data: { name: locations[i][0] } }); marker.addListener('click', function() { if(!this.infoWindow) { this.infoWindow = new google.maps.InfoWindow({ content: this.data.name; }); } this.infoWindow.open(map,this); }) }
异步版本:
<script type="text/javascript"> function initialize() { var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; var map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: new google.maps.LatLng(-33.92, 151.25), mapTypeId: google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(locations[i][0]); infowindow.open(map, marker); } })(marker, i)); } } function loadScript() { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' + 'callback=initialize'; document.body.appendChild(script); } window.onload = loadScript; </script>
的 资源 链接 强>
的 演示 链接 强>
的 完整的HTML代码 强>
<!DOCTYPE html> <html> <head> <style> /* <span class="metadata-marker" style="display: none;" data-region_tag="css"></span> Set the size of the div element that contains the map */ #map { height: 400px; /* The height is 400 pixels */ width: 100%; /* The width is the width of the web page */ } </style> <script> var map; var InforObj = []; var centerCords = { lat: -25.344, lng: 131.036 }; var markersOnMap = [{ placeName: "Australia (Uluru)", LatLng: [{ lat: -25.344, lng: 131.036 }] }, { placeName: "Australia (Melbourne)", LatLng: [{ lat: -37.852086, lng: 504.985963 }] }, { placeName: "Australia (Canberra)", LatLng: [{ lat: -35.299085, lng: 509.109615 }] }, { placeName: "Australia (Gold Coast)", LatLng: [{ lat: -28.013044, lng: 513.425586 }] }, { placeName: "Australia (Perth)", LatLng: [{ lat: -31.951994, lng: 475.858081 }] } ]; window.onload = function () { initMap(); }; function addMarkerInfo() { for (var i = 0; i < markersOnMap.length; i++) { var contentString = '<div id="content"><h1>' + markersOnMap[i].placeName + '</h1><p>Lorem ipsum dolor sit amet, vix mutat posse suscipit id, vel ea tantas omittam detraxit.</p></div>'; const marker = new google.maps.Marker({ position: markersOnMap[i].LatLng[0], map: map }); const infowindow = new google.maps.InfoWindow({ content: contentString, maxWidth: 200 }); marker.addListener('click', function () { closeOtherInfo(); infowindow.open(marker.get('map'), marker); InforObj[0] = infowindow; }); // marker.addListener('mouseover', function () { // closeOtherInfo(); // infowindow.open(marker.get('map'), marker); // InforObj[0] = infowindow; // }); // marker.addListener('mouseout', function () { // closeOtherInfo(); // infowindow.close(); // InforObj[0] = infowindow; // }); } } function closeOtherInfo() { if (InforObj.length > 0) { /* detach the info-window from the marker ... undocumented in the API docs */ InforObj[0].set("marker", null); /* and close it */ InforObj[0].close(); /* blank the array */ InforObj.length = 0; } } function initMap() { map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: centerCords }); addMarkerInfo(); } </script> </head> <body> <h3>My Google Maps Demo</h3> <!--The div element for the map --> <div id="map"></div> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script> </body> </html>