我设计了谷歌地图,以标记的形式显示多个位置。现在我的要求是,每当我单击任何标记时,出现在信息窗口中的其内容应该存储到某个变量中,并且可以在重新单击任何标记时更新该变量的内容。
这是我使用的代码....
谷歌地图多个标记
变量信息变量;var locarr =[]; locarr[0]="Khilwat,Charminar,海得拉巴";
locarr[1]="Lower Tank Bund Rd, Hyderabad";geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(-34.397, 150.644); var mapOptions = { zoom: 10, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); var bounds = new google.maps.LatLngBounds(); for (var i = 0; i < locarr.length; i++) { var Mycontent=locarr[i]; (function(Mycontent) { geocoder.geocode({ 'address': locarr[i] }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); var infowindow = new google.maps.InfoWindow(); infowindow.setContent(Mycontent); google.maps.event.addListener(marker, 'click', (function(marker, i,Mycontent) { return function(Mycontent) { infowindow.open(map, marker); } })(marker, i,Mycontent)); bounds.extend(marker.getPosition()); } //if ends else { alert('Geocode was not successful for the reason: ' + status); } });// geocode ends here :- })(Mycontent);//anonymous func ends here } //for ends
在此先感谢您的帮助 !