-1

well I'm beginner on google map v3, so my problem now is how to add multiple markers on map when I click on a button that I put on the map. When I filled the matrice manually, the multiple markers are shown on the map but if I use variables from database only the last longitude and latitude are shown on map :( this is my javascript code :

//loading longitude and latitude from database
for(var i=0;i<len;i++){

       var locations = [[results.rows.item(i).lat,results.rows.item(i).long]];           
 }



//click event
       google.maps.event.addDomListener(myControl, 'click', function() {

for(var i=0;i<len;i++){

//creation of markers on map
      var marker = new google.maps.Marker({
      position: new google.maps.LatLng(locations[i][0], locations[i][1]),
      map: map,
      title: "num:"+i,


  });}


  });
Steven V
  • 16,357
  • 3
  • 63
  • 76
iteb khayati
  • 107
  • 1
  • 10

1 Answers1

2

Javascript doesn't have multidimensional arrays, so you'll need to use an array of arrays.

Here is an example:

var matrix =
  [
    [1, 0, 4, 3],
    [2, 3, 8, 6],
    [9, 7, 2, 2]
  ];

// matrix[1][2] === 8

Update

//loading longitude and latitude from database
var locations = [];
for(var i=0;i<len;i++){
    locations.push([results.rows.item(i).lat,results.rows.item(i).long]);           
}
Paul
  • 139,544
  • 27
  • 275
  • 264
  • Paulpro thanks for response :) Can I declare it like this : var locations = [ [results.rows.item(i).lat,results.rows.item(i).long] ]; – iteb khayati Apr 22 '14 at 18:03
  • results.rows.item(i).lat / results.rows.item(i).long ---> two variables contains longitude and latitude for a place and locations --> the name of matrice – iteb khayati Apr 22 '14 at 18:04
  • @user3554933 Yes you can do that, you probably want to use `push` if you are doing that in a loop though. Before the loop you should declare an empty array: `var locations = [];` – Paul Apr 22 '14 at 18:05
  • @user3554933 Then in the loop you should push the location onto the array: `locations.push([results.rows.item(i).lat, results.rows.item(i).long]);`. – Paul Apr 22 '14 at 18:07
  • You may also want to use an array of objects instead: `locations.push({lat:results.rows.item(i).lat, long:results.rows.item(i).long});`. – Paul Apr 22 '14 at 18:08
  • If you use that you would access the properties later like `locations[i].lat` – Paul Apr 22 '14 at 18:08
  • I 'll edit my question to more explain my problem Paulpro :) – iteb khayati Apr 22 '14 at 18:31
  • @user3554933 I added an update. Change your first loop to that. – Paul Apr 22 '14 at 18:42
  • thannnnnnnnkkk youuu very much bro :D – iteb khayati Apr 22 '14 at 18:59