[英]How to add an array to an array in javascript?
I will be getting coordinates
as 我将获得
coordinates
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?
这怎么可能?
have you tried : 你有没有尝试过 :
for(var i = 0 ; i < polygonBounds.length ; i++)
{
coordinates.push([polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng()]);
}
(notice the '[' and ']')? (注意'['和']')?
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);
}
(25.774252, -80.190262)
is not a valid value, so it can't be stored in array. (25.774252, -80.190262)
不是有效值,因此不能存储在数组中。
You must use array or object instead of it. 您必须使用数组或对象而不是它。
[25.774252, -80.190262]
or {lat: 25.774252, lng: -80.190262}
[25.774252, -80.190262]
或{lat: 25.774252, lng: -80.190262}
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;
并得到
var some = coordinates[0].x;
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.