Amanda_Panda
Amanda_Panda

Reputation: 1186

How to create an array of Google Maps Geocoordinates

In Javascript, I have two arrays, called latitude and longitude, that have appropriately long and lat values (as strings originally from an asp.net controller, I got them from parsing an XML feed).

What I want to do is create a new array, called points, that has the lat/lng values in it, so I can display those values on a map. Here is what I thought should work, but does absolutely nothing:

       var points=new Array();

       for(var i=0; i<latitude.length; i++){
       points=new google.maps.LatLng(latitude[i], longitude[i]);
       }

the latitude and longitude arrays are the same length, I don't think thats a problem. Thanks, Amanda

Upvotes: 0

Views: 5226

Answers (1)

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91379

You are not adding elements to the array, but instead assigning a new LatLng object to the points variable in each loop iteration (points = new). To add elements to the array, either subscript the array position (points[index] = value) and assign the object to it:

var points=new Array();
for (var i=0; i < latitude.length; i++) {
  points[i] = new google.maps.LatLng(latitude[i], longitude[i]);
}

Or use Array#push():

var points = [];
for (var i=0; i < latitude.length; i++) {
  points.push(new google.maps.LatLng(latitude[i], longitude[i]));
}

Upvotes: 3

Related Questions