I need to build an array with a collection of co-ordinates.. the example shows the array built like this..
stops = [{"Geometry":{"Latitude":51.507937,"Longitude":-0.076188}},
{"Geometry":{"Latitude":51.51168,"Longitude":-0.114155}},
{"Geometry":{"Latitude":51.5010063,"Longitude":-0.041407}}] ;
I am trying to build a loop that reads co-ords from elsewhere and pushes them in the stops array.. this is what i have so far but i know its wrong..
var x = document.getElementsByClassName("postcode");
for (i = 0; i < x.length; i++) {
postcode = x[i].innerText;
lon = getLon(postcode);
lat = getLat(postcode);
myarray = [{"Latitude":lon,"Longitude":lat}];
stops.push([{"Geometry":myarray}]);
}
Evan Davis
36.7k7 gold badges53 silver badges58 bronze badges
asked May 19, 2015 at 19:09
user4707267
-
"but i know its wrong" How do you know that? What's wrong?Felix Kling– Felix Kling2015年05月19日 19:25:43 +00:00Commented May 19, 2015 at 19:25
-
because if I dump the array it looks completely different to the one i need in the first bit of code.user4707267– user47072672015年05月20日 18:11:49 +00:00Commented May 20, 2015 at 18:11
-
How does it look like?Felix Kling– Felix Kling2015年05月20日 18:18:40 +00:00Commented May 20, 2015 at 18:18
1 Answer 1
Replace
myarray = [{"Latitude":lon,"Longitude":lat}];
stops.push([{"Geometry":myarray}]);
with
var myobject = {"Latitude":lon,"Longitude":lat};
stops.push({"Geometry":myobject});
Add also the missing var keyword in the for loop:
for (var i = 0; i < x.length; i++) {
When you don't, it makes the i variable global, which usually leads to painful bugs.
answered May 19, 2015 at 19:11
Denys Séguret
384k90 gold badges813 silver badges780 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js