从字符串中解析所有long / lat

时间:2011-02-15 14:33:17

标签: javascript

您好我正试图获得以这种格式到达的每个经度和纬度:

(52.99315484540412, -1.179145092694469)(52.99315323095451, -1.1786797294303852)(52.993042641012025, -1.1789238104507405)(52.99308461678997, -1.1791705736801106)

我希望将它们存储在这些字符串tmpLon1 tmpLat1 tmpLon2 tmpLat2 tmpLon3 tmpLat3 tmpLon4 tmpLat4

到目前为止,我已完成此编码,但它无法正常工作,任何机构都有更好的方法,可以编写一个示例吗?

//(52.99315484540412, -1.179145092694469)(52.99315323095451, -1.1786797294303852)(52.993042641012025, -1.1789238104507405)(52.99308461678997, -1.1791705736801106)

//Remove all the shit
longlat = (longlat.replace(/\(/g,""));
longlat = (longlat.replace(/\)/g,"|"));
longlat = (longlat.replace(/\ /g,""));

//52.99315484540412,-1.179145092694469|52.99315323095451, -1.1786797294303852|52.993042641012025, -1.1789238104507405|52.99308461678997, -1.1791705736801106|

//Now split out each long lat
tmpLon1 = longlat.split(",","1"); //ok
tmpLat1 = longlat.replace(tmpLon1 + ",","");
tmpLon2 = tmpLat1;
tmpLat1 = tmpLat1.split("|","1"); //ok
tmpLon2 = tmpLon2.split("|","2");
tmpLon2 = tmpLon2.replace(tmpLat1,"");

tmpLat2 = longlat.split(",","4");

tmpLon3 = "";
tmpLat3 = "";

tmpLon4 = "";
tmpLat4 = "";   

5 个答案:

答案 0 :(得分:3)

我会这样做:

var a = "(52.99315484540412, -1.179145092694469)(52.99315323095451, -1.1786797294303852)(52.993042641012025, -1.1789238104507405)(52.99308461678997, -1.1791705736801106)",
    latLngs = [], 
    pairs = a.replace(/^\(|\)$/g,'').split(')(');

for(var i=0,pair;pair=pairs[i];i++) {
  pair = pair.split(',');
    latLngs.push({lat: +pair[0], lng: +pair[1]});  
}
console.log(latLngs);

你可以在这里看到它:http://jsfiddle.net/LCYrg/

您也可以这样做:

var a = "(52.99315484540412, -1.179145092694469)(52.99315323095451, -1.1786797294303852)(52.993042641012025, -1.1789238104507405)(52.99308461678997, -1.1791705736801106)",

JSON.parse(a.split(')(').join('],[').replace('(','[[').replace(')',']]'));

但这需要存在JSON对象,而在ie7及以下则不然。这可以通过包含这里找到的json2.js文件来修复:https://github.com/douglascrockford/JSON-js

答案 1 :(得分:1)

正规救援!由于您知道字符串的格式,因此可以在此处使用正则表达式来搜索。

var str = "(52.99315484540412, -1.179145092694469)(52.99315323095451, -1.1786797294303852)(52.993042641012025, 1.1789238104507405)(52.99308461678997, -1.1791705736801106)";

var regex = /\((-?[0-9]+\.[0-9]+), (-?[0-9]+\.[0-9]+)\)/g;
var latlonArray = [];

var match = regex.exec(str);
while (match) {
    latlonArray.push({
        "lat" : match[1],
        "lon" : match[2]
    });
    match = regex.exec(str);
}

var tmpLon1 = latlonArray[0].lon;
var tmpLat1 = latlonArray[0].lat;

var tmpLon2 = latlonArray[1].lon;
var tmpLat2 = latlonArray[1].lat;

var tmpLon3 = latlonArray[2].lon;
var tmpLat3 = latlonArray[2].lat;

var tmpLon4 = latlonArray[3].lon;
var tmpLat4 = latlonArray[3].lat;

示例:http://jsfiddle.net/jonathon/57rE4/

latlonArray是一个包含latlon属性的对象数组,包含在字符串中找到的属性。

注意:我在这里使用的正则表达式只是我想出的东西。它适用于您的示例输入,但可能不一定是最好的。

另请注意:这并不意味着“拔出并使用”JavaScript。它只是向您展示了另一种方法。我建议通过各种方式对其进行优化(例如将所有变量移到顶部等)。

答案 2 :(得分:0)

我猜你可以这样做: (假设输入是您的输入字符串)

input = input.replace(/\s/,''); //remove spaces
var pairs_strings = input.substring(1,-1).split(")("); //remove braket of start and end and then split into pairs
var pairs = new Array();

for(var i=0; i<paris_strings.length; i++){
    lon = pairs_strings[i].split(",").shift();
    lat = pairs_strings[i].split(",").pop();
    pairs[i] = new Array(lon, lat);
}

var tmpLon1 = pairs[0][0];
var tmpLan1 = pairs[0][1];
....

但要注意它没有经过测试,我不知道负值会发生什么

答案 3 :(得分:0)

如果您的输入格式没有改变,您可以分三行进行:

var str = "(52.99315484540412, -1.179145092694469)(52.99315323095451, -1.1786797294303852)(52.993042641012025, -1.1789238104507405)(52.99308461678997, -1.1791705736801106)";   
var pattern = /([\d\.-]+),\s([\d\.-]+)\)\(([\d\.-]+),\s([\d\.-]+)\)\(([\d\.-]+),\s([\d\.-]+)\)\(([\d\.-]+),\s([\d\.-]+)/;
var result = str.match(pattern);

要使用结果:

tmpLon1=result[1];
tmpLon2=result[3];
tmpLon3=result[5];
tmpLon4=result[7];

tmpLat1=result[2];
tmpLat2=result[4];
tmpLat3=result[6];
tmpLat4=result[8];

答案 4 :(得分:0)

var str = "(52.99315484540412, -1.179145092694469)(52.99315323095451, -1.1786797294303852)(52.993042641012025, -1.1789238104507405)(52.99308461678997, -1.1791705736801106)";
var str = str.replace(/\)\(/g,', ').replace(/\(|\)/g,'').split(", ");

获得结果:

tmpLon1=result[0];
tmpLon2=result[2];
tmpLon3=result[4];
tmpLon4=result[6];

tmpLat1=result[1];
tmpLat2=result[3];
tmpLat3=result[5];
tmpLat4=result[7];