2010-06-23 6 views
2

Je suis assez nouveau sur google maps alors soyez patient!Google Maps API V3 - besoin d'aide avec plusieurs marqueurs

Mon code fonctionnait très bien jusqu'à ce que j'ai essayé d'ajouter plusieurs marqueurs, apprécierais vraiment si quelqu'un pouvait coup d'œil et voir si je manque quelque chose ...

$(function() { 

    var map_markers = [ [ [52.951946], [1.018124] ], [ [52.955311], [0.987997] ] ]; 

    var options = { 
     zoom: 13, 
     center: latlng, 
     mapTypeControl: false, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    };   
    var map = new google.maps.Map(document.getElementById('map_canvas'), options); 


    //markers    
    for (var i = 0; i < map_markers.length; i++) { 
     var m =   map_markers[i]; 
     var myLatLng = new google.maps.LatLng(m[0], m[1]); 
     var marker = new google.maps.Marker({ 
      position: myLatLng, 
      map: map 

     }); 
    } 
}) 

Répondre

2

Votre problème est de savoir comment vous initialisé votre tableau map_markers. Vous devriez probablement faire ce qui suit:

var map_markers = [[52.951946, 1.018124], [52.955311, 0.987997]]; 

Sinon, votre map_markers vous auriez dû faire référence comme suit:

var m =   map_markers[i]; 
var myLatLng = new google.maps.LatLng(m[0][0], m[0][1]); 

Brisons votre tableau map_markers pour vous aider à comprendre le problème:

var map_markers = [ [ [52.951946], [1.018124] ], [ [52.955311], [0.987997] ] ]; 

console.log(map_markers[0]); 
// [[52.951946], [1.018124]] 

Object.prototype.toString.call(map_markers[0]); 
// "[object Array]" 

console.log(map_markers[0][0]); 
// [52.951946]     

Object.prototype.toString.call(map_markers[0][0]); 
// "[object Array]" 

console.log(map_markers[0][0][0]); 
// 52.951946      

Object.prototype.toString.call(map_markers[0][0][0]); 
// "[object Number]" 

Par conséquent, le problème que vous aviez se résume au fait que les paramètres que vous passiez à la google.maps.LatLng() constructeur étaient un Array au lieu d'un Number.

+0

merci, ceci est une excellente réponse – Haroldo

Questions connexes