0

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}]);

}
3
  • "but i know its wrong" How do you know that? What's wrong? Commented 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. Commented May 20, 2015 at 18:11
  • How does it look like? Commented May 20, 2015 at 18:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.