2012-01-05 82 views
0

我想每個標記都有不同的圖標。 我的代碼有問題。只使用陣列的最後一項。 如果查詢數組有3個項目,所有3個標記都會有3.png圖標。谷歌地圖API,每個標記的相同圖標

感謝您的幫助!

 var img = new Array(); 
     img.push("'images/1.png", "images/2.png", "images/3.png", "images/4.png", "images/5.png", "images/6.png", "images/7.png", "images/8.png", "images/9.png"); 

     var myOptions = { 
      scaleControl: true, 
      streetViewControl: false, 
      zoom: 12, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     var map = new google.maps.Map(document.getElementById('map_canvas'), 
     myOptions); 

     var markers = new Array(query.length); 

     for (i=0;i<query.length;i++) 
     { 
      var geocoder = new google.maps.Geocoder(); 
      var address = query[i]; 

      var image = new google.maps.MarkerImage(
      img[i], 
      new google.maps.Size(24,24), 
      new google.maps.Point(0,0), 
      new google.maps.Point(24,24) 
      ); 

      geocoder.geocode({ 'address': address }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        map.setCenter(results[0].geometry.location); 
        markers[i] = new google.maps.Marker({ 
         title:"Marker "+i, 
         icon: image, 
         map: map, 
         position: results[0].geometry.location 
        }); 
        markers[i].setMap(map); 
       } else { 
        alert("Geocode was not successful for the following reason: " + status); 
       } 
      }); 


     }; 

    } 

    google.maps.event.addDomListener(window, 'load', initialize); 

回答

0

似乎你總是在傳遞給geocode回調相同i。 您可能想嘗試一種解決方案,如suggested

1

訪問谷歌的地理編碼服務是異步

這應該解決您的問題,

var img = new Array(); 
    img.push("'images/1.png", "images/2.png", "images/3.png", "images/4.png", "images/5.png", "images/6.png", "images/7.png", "images/8.png", "images/9.png"); 

    var myOptions = { 
     scaleControl: true, 
     streetViewControl: false, 
     zoom: 12, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    var map = new google.maps.Map(document.getElementById('map_canvas'), 
    myOptions); 

    var markers = new Array(query.length); 
    var images = new Array(query.length); 

    for (i=0;i<query.length;i++) 
    { 
     var geocoder = new google.maps.Geocoder(); 
     var address = query[i]; 

     var image = new google.maps.MarkerImage(
     img[i], 
     new google.maps.Size(24,24), 
     new google.maps.Point(0,0), 
     new google.maps.Point(24,24) 
     ); 

     images[i] = image; 

     geoCode(i); 

    }; 

    function geoCode(i){ 

      geocoder.geocode({ 'address': address }, function (results, status) { 

      if (status == google.maps.GeocoderStatus.OK) { 
       map.setCenter(results[0].geometry.location); 
       markers[i] = new google.maps.Marker({ 
        title: results[0].formatted_address, 
        icon: images[i], 
        map: map, 
        position: results[0].geometry.location 
       }); 
       markers[i].setMap(map); 
      } else { 
       alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 
    } 


} 

google.maps.event.addDomListener(window, 'load', initialize);