2012-05-21 16 views
5

Sto provando a posizionare più marker su una mappa fornita da una matrice. In questo momento solo i miei punti point iniziali (NYC).posizionando più marker su google map dall'array

var geocoder; 
var map; 
var markersArray = []; 

//plot initial point using geocode instead of coordinates (works just fine) 
    function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    latlang = geocoder.geocode({ 'address': 'New York City'}, function(results, status) { //use latlang to enter city instead of coordinates 
      if (status == google.maps.GeocoderStatus.OK) { 
       map.setCenter(results[0].geometry.location); 
       marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
       }); 
      markersArray.push(marker); 
      } 
      else{ 
      alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 
    var myOptions = { 
     center: latlang, zoom: 5, mapTypeId: google.maps.MapTypeId.SATELLITE, 
     navigationControlOptions: { 
      style: google.maps.NavigationControlStyle.SMALL 
     } 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 

    } 

/////////////////////////////////////////////////////////// 
//Everything below this line is for attempting to plot the markers 

    var locationsArray = ['Pittsburgh','Chicago', 'Atlanta']; 

    function plotMarkers(){ 
for(var i = 0; i < locationsArray.length; i++){ 
    codeAddresses(locationsArray[i]); 
} 
    } 

    function codeAddresses(address){ 
    geocoder.geocode({ 'address': address}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       map.setCenter(results[0].geometry.location); 
       marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
       }); 
      //markersArray.push(marker); 
      } 
      else{ 
      alert("Geocode was not successful for the following reason: " + status); 
      } 
    }); 
    } 
+1

è necessario fornire ulteriori informazioni sul problema si ottiene qualche eccezione? – Jorge

risposta

5

Non sta effettivamente chiamando plotMarkers in qualsiasi parte del frammento di cui sopra! Quando ho aggiunto alla fine dell'inizializzazione (dopo che la mappa è stata definita) funziona alla grande! http://jsfiddle.net/T5aKE/

 ... 
     map = new google.maps.Map... 
     plotMarkers(); 
     ... 
+0

Wow, non posso credere di aver perso un errore così ovvio. Mi sembra sempre di aver bisogno di un secondo set di occhi! Grazie! – user1104854

Problemi correlati