0

I will be getting coordinates as

25.774252, -80.190262

18.466465, -66.118292

32.321384, -64.75737

I have to maintain an array like

Coordinates[] = [(25.774252, -80.190262),(18.466465, -66.118292),(32.321384, -64.75737)]

How is this possible? Or is there any other method to get latitude and longitude point by point?

I tried like,

for(var i = 0 ; i < polygonBounds.length ; i++)
{
     coordinates.push(polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng()); 
}

but it will be like,

0
    31.796473239688435

1
    -106.51227951049805

2
    31.797786324219413

3
    -106.49425506591797

4
    31.78392504670159

5
    -106.47829055786133

6
    31.757509914027327

7
    -106.48704528808594

8
    31.776191009772532

9
    -106.52069091796875

10
    31.790782991145434

11
    -106.5208625793457

So now i have this array i need to take each latitude and longitude by looping the same array. How is it possible?

2
  • You want coordinates.push([polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng()]);? Commented Feb 25, 2014 at 13:51
  • if you want to push an array: .push([val1, val2]) Commented Feb 25, 2014 at 13:53

5 Answers 5

1

if you want to push pairs, push an array:

for (var i = 0; i < polygonBounds.length; i++) {
     coordinates.push([polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng()]); 
}

it would be the short for:

for (var i = 0; i < polygonBounds.length; i++) {
     var coords = [];
     coords.push(polygonBounds.getAt(i).lat());
     coords.push(polygonBounds.getAt(i).lng());
     coordinates.push(coords); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

have you tried :

for(var i = 0 ; i < polygonBounds.length ; i++)
{
     coordinates.push([polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng()]); 
}

(notice the '[' and ']')?

Comments

0

(25.774252, -80.190262) is not a valid value, so it can't be stored in array.

You must use array or object instead of it.

[25.774252, -80.190262] or {lat: 25.774252, lng: -80.190262}

Comments

0

try at

for(var i = 0 ; i < polygonBounds.length ; i++)
{
    var obj = {};
    obj.x = polygonBounds.getAt(i).lat();
    obj.y = polygonBounds.getAt(i).lng();
    coordinates.push(obj); 
}

and get var some = coordinates[0].x;

Comments

0

What you need is an array of objects where every object stores two properties

var coordinates = [];
for(var i=0; i<polybounds.length; i+=2) {
    coordinates.push({ lat: polybounds[i], long: polybounds[i+1] });
}

Now you can do anything on coordinates with something like,

for(var i=0; i<coordinates.length; i+=2) {
    var long = coordinates[i].long;
    var lat = coordinates[i].lat;
    console.log(lat + ',' + long);
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.