複数の場所をマーカーの形で表示する Google マップを設計しました。ここで私の要件は、マーカーをクリックするたびに、情報ウィンドウに表示されるそのコンテンツを変数に保存し、変数のコンテンツをマーカーの再クリックで更新できるようにすることです。
これが私が使用したコードです....
Google マップの複数のマーカー
var info_var; var locarr =[]; locarr[0]="キルワット、チャーミナール、ハイデラバード";
locarr[1]="Lower Tank Bund Rd, ハイデラバード";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
よろしくお願いいたします。