开发者

Parse out all long/lat from a string

Hi i am trying to get each longitude and latitude that arrives in this format:

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

I wish to store them in these strings tmpLon1 tmpLat1 tmpLon2 tmpLat2 tmpLon3 tmpLat3 tmpLon4 tmpLat4

So far i have done this coding but it fails to work, any body have a better method and could code up an example please ?

//(52.99315484540412, -1.179145092694469)(52.993153230开发者_JAVA百科95451, -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 = "";   


I'd do this:

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);

you can see it live here: http://jsfiddle.net/LCYrg/

You could also simply do:

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(')',']]'));

But this would require the JSON object to be present, which is not the case in ie7 and below. This could be fixed by including the json2.js file found here: https://github.com/douglascrockford/JSON-js


Regex to the rescue! Since you know the format of your string, you can use regex here to search for you.

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;

Example: http://jsfiddle.net/jonathon/57rE4/

The latlonArray is an array of objects with lat and lon properties, containing the ones found in your string.

Note: the regex I used here is just something I came up with. It works with your example input but might not necessarily be the best.

Also note: this isn't meant to be 'pull out and use' JavaScript. It's merely showing you another way to do it. I'd recommend optimising it by various means (e.g. moving all vars to the top, etc).


i guesy u can do something like: (assuming input is your input string)

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];
....

but be aware it's not tested and i don't know what will happend with the negative values


If your input format is not changing you could do it in 3 lines:

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);

To get the results use:

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

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


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(", ");

To get the results:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜