2012-05-21 2 views
5

J'essaie de placer plusieurs marqueurs sur une carte qui sont fournis à partir d'un tableau. À l'heure actuelle seulement mon point de chargement initial (NYC).placer plusieurs marqueurs sur google map à partir du tableau

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

Vous devez fournir plus d'informations à propos de votre problème vous avez une exception? – Jorge

Répondre

5

Vous n'appelez pas réellement plotMarkers n'importe où dans l'extrait ci-dessus! Lorsque j'ai ajouté à la fin de l'initialisation (après la définition de la carte), cela fonctionne très bien! http://jsfiddle.net/T5aKE/

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

Wow, je ne peux pas croire que j'ai manqué une erreur aussi évidente. Je semble toujours avoir besoin d'une deuxième paire d'yeux! Je vous remercie! – user1104854

Questions connexes