Question

I need to create an array of the following in order to create a heatmap effect:

var heatmapData = [  new google.maps.LatLng(*latitude*, *longitude*) ];

Obviously the final array will have much more data. Right now I have two arrays, one containing all of the latitude points and one containing all of the longitude points. I'm wondering if there is a way to loop through both arrays when populating the heatmapData array?

I've thought about:

for(var i = 0 ; i < latitudeArray.length ; i++){
      heatmapData.push(new google.maps.LatLng(latitudeArray[i]))
      };

But then I get stuck as to how I'd introduce the second argument from the longitudeArray into the google.maps.LatLng.

Does anybody know how I can loop through both arrays? Please let me know. Thanks!

Was it helpful?

Solution

I assume that the arrays are both the same length. In which case you can simply do:

for (var i = 0, l = latitudeArray.length; i < l; i++) {
  heatmapData.push(new google.maps.LatLng(latitudeArray[i], longitudeArray[i]));
}

Note that it's often better to declare your array length as a variable, in this case l, so that the loop doesn't keep referencing the array length.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top