2014-01-22 221 views
-1

我需要在地圖上顯示標記,併爲每個標記顯示一些帶有一些信息的彈出窗口。 這裏我的代碼:Google地圖和地圖上的標記

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
    var createMap = function() { 
     var address = document.getElementById("address").value; 
     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({'address': address}, function(results,status){ 
      if (status == google.maps.GeocoderStatus.OK) { 
       var options = { 
        zoom: 12, 
        center: results[0].geometry.location, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }; 
       var map = new google.maps.Map(document.getElementById('map'), options); 
       setMarkers(map, sites); 
      } 
      else { 
       alert("Problema nella ricerca dell'indirizzo: " + status); 
      } 
     }); 
    } 

    //Aggiungo i marker alla mappa 
    function setMarkers(map, markers) { 
     for (var i = 0; i < markers.length; i++) { 
      var sites = markers[i]; 
      var siteLatLng = new google.maps.LatLng(sites[1], sites[2]); 
      var marker = new google.maps.Marker({ 
       position: siteLatLng, 
       map: map, 
       title: sites[0], 
       zIndex: sites[3], 
       html: "<b>"+sites[0]+"</b><br>"+sites[4] 
      }); 

      google.maps.event.addListener(marker, "click", function() {    
       infowindow.setContent(this.html); 
       infowindow.open(map, this); 
      }); 
     } 
    } 

    //Elenco dei marcatori da aggiungere 
    var sites = [[ 'Veterinari Associati', 45.448405,9.19823 , 1 , 'Via Palladio 4 - 20100 Milano (MI)'],]; 


    window.onload = createMap; 
</script> 
<input id="address" type="hidden" value="Milan"> 
<div id="map" style="width:345px; height:306px;"></div> 

一切正常,標記的位置,但是當我點擊了市場上彈出doen't顯示出來。 我無法理解問題出在哪裏......

+0

我不認爲this.html == marker.html的 –

+0

可能重複[谷歌地圖JS API V3 - 簡單多個標記示例] (http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example) – geocodezip

回答

2

你力intialise信息窗口,試試這個

google.maps.event.addListener(marker, "click", function() { 
    var infowindow = new google.maps.InfoWindow(); 
    infowindow.setContent(this.html); 
    infowindow.open(map, this); 
}); 
+0

謝謝!我總是想念它! –

+0

歡迎您! –

1

這是因爲在設置HTML並打開它之前,您尚未設置infowindow。插入此:

var infowindow = new google.maps.InfoWindow(); 
+0

謝謝!我總是想念它! –