2015-05-14 62 views
-1

我一直在這一整天擺弄這一切,只是似乎無法弄清楚。當我渲染地圖時,每次點擊某個點時,都會打開添加的最後一個位置的windowInfo。我已經驗證每個XML元素返回它的正確的名稱,位置,經緯度等。任何幫助將不勝感激。Google Maps API JS All Points打開同樣的infoWindow點擊

<code> 
     <script type="text/javascript" src="//maps.googleapis.com/maps/api/js"></script> 
     <script> 
      $(document).ready(function() { 
       $("#map").css({ 
        height: 300, 
        width: 400 
       }); 
       var myLatLng = new google.maps.LatLng(33.985236, -117.715781); 
       MYMAP.init('#map', myLatLng, 8); 

       //alert("0: "+MYMAP.map.getZoom()); 

       MYMAP.placeMarkers('/admin/company/project/googlexml'); 

       //alert("2: "+MYMAP.map.getZoom()) 


      }); 


      var wh = $(document).height(); 
      //$('#frame').height(wh - 125); 

      google.maps.visualRefresh = true; 
      var locations = 0; 

      var MYMAP = { 
       map: null, 
       bounds: null 
      } 




      MYMAP.init = function (selector, latLng, zoom) { 
       var myOptions = { 
        zoom: zoom, 
        center: latLng, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       } 
       this.map = new google.maps.Map($(selector)[0], myOptions); 
       this.bounds = new google.maps.LatLngBounds(); 

       //alert("0: "+MYMAP.map.getZoom()); 

       window.setTimeout(function() { 
        //alert("10: "+MYMAP.map.getZoom()); 
        if (MYMAP.map.getZoom() > 13) { 
         MYMAP.map.setZoom(13) 
        } 
       }, 500); 

      } 

      MYMAP.placeMarkers = function (filename) { 
       $.post(filename, function (xml) { 
        var markers = xml.documentElement.getElementsByTagName("marker"); 
        for (var i = 0; i < markers.length; i++) { 
         var name = markers[i].getAttribute("name"); 
         var address = markers[i].getAttribute("address"); 
         //var image = '/images/someimage.png'; 
         var lat = markers[i].getAttribute("lat"); 
         var lng = markers[i].getAttribute("lng"); 
         var point = new google.maps.LatLng(
           parseFloat(lat), 
           parseFloat(lng) 
           ); 

         //alert("data :" + name + " Address " + address + " lat/lng " + lat + ":" + lng); 

         locations++; 

         // extend the bounds to include the new point 

         MYMAP.bounds.extend(point); 

         var marker = new google.maps.Marker({ 
          position: point, 
          map: MYMAP.map, 
          //icon: image, 
          clickable: true 
         }); 

         marker.info = new google.maps.InfoWindow({ 
          content: '<strong>' + name + '</strong><br />' + address 
         }); 

         google.maps.event.addListener(marker, 'click', function() { 
          marker.info.open(MYMAP.map, marker); 
         }); 


         MYMAP.map.fitBounds(MYMAP.bounds); 

         //alert("1: " + MYMAP.map.getZoom()); 
        } 
       }, 'xml'); 
      } 
</code> 
+0

可能重複的 - 簡單多標記示例](http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example) – geocodezip

回答

0

使用「本」在你的監聽click事件,「標誌」仍包含最後一個值它在循環:

google.maps.event.addListener(marker, 'click', function() { 
    this.info.open(MYMAP.map, marker); 
}); 
[谷歌地圖JS API V3的
相關問題