我需要取一串坐标并从中创建任何数组。
var points = [];
points[0] = [-122.349517923,47.975040441];
points[1] = [-122.349518007,47.975040609];
points[2] = [-122.349758148,47.975320816];
points[3] = [-122.349972725,47.975363731];
points[4] = [-122.350101471,47.975342274];
points[5] = [-122.351560593,47.974805832];
这是我目前拥有的
var path = "-122.349517923,47.975040441 -122.349518007,47.975040609 -122.349758148,47.975320816 -122.349972725,47.975363731 -122.350101471,47.975342274 -122.351560593,47.974805832";
var points =[];
var patharray = path.split(' ');
for(i = 0; i < patharray.length; ++i) {
points = [[patharray[i]]];
}
console.log(patharray);
console.log(points[0]);
console.log(points[1]);
console.log(points[2]);
console.log(points[3]);
答案 0 :(得分:0)
你的第一次&#34;分裂&#34;没问题,但是你需要再次拆分&#39;,&#39;,并将字符串转换为数字:
var path = "-122.349517923,47.975040441 -122.349518007,47.975040609 -122.349758148,47.975320816 -122.349972725,47.975363731 -122.350101471,47.975342274 -122.351560593,47.974805832";
var points = [];
var patharray = path.split(' ');
for(i = 0; i < patharray.length; ++i) {
var strCoords = patharray[i].split(',');
points.push(strCoords.map(function(s) { return +s; }));
}