如何将一串地图坐标转换为数组javascript?

时间:2017-06-24 23:08:06

标签: javascript arrays google-maps

我在地图上绘制路径,路径的坐标以jtring文件中的字符串保存,格式如下: (43.886758784865066,24.226741790771484),(43.90271630763887,24.234981536865234)

我需要获取这些值并将它们添加到数组中: coordinates = [43.886758784865066,24.226741790771484,43.90271630763887,24.234981536865234];

那么我该怎么做呢?

2 个答案:

答案 0 :(得分:0)

您可以使用正则表达式来解析这些字符串。

const match = string.match(/\((.*)\, (.*)\),\((.*)\, (.*)\)/)
/*
  Matches
  ["(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)", "43.886758784865066", "24.226741790771484", "43.90271630763887", "24.234981536865234"]
*/
const Array.prototype.slice.call(match).splice(1, 4)
/* Converts to array and takes the last three elements 
["43.886758784865066", "24.226741790771484", "43.90271630763887", "24.234981536865234"]
*/

答案 1 :(得分:0)

你可以这样试试

var string = '(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)';
string.match(/\d+(\.\d+)/g).map(function(d){return d;});