Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract lat/lng coordinates from a string

I’ve got a string which contains several coordinates. It is just random human readable text. For example:

Bla bla bla 35.45672,-162.91938 yadda yadda yadda -6.53197, 132.07407 bla bla

I would like to extract all the coordinates from the text to an array. The coordinates are seperated by a comma, or a comma and a space (As in the example above). There is no fixed length or seperators. There is no fixed amount of coordinates. It is just random text containing lat/lng coordinates.

What would be the best way to extract all coordinates from the text into an array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use match() here with an appropriate regex pattern:

var input = "Bla bla bla 35.45672,-162.91938 yadda yadda yadda -6.53197, 132.07407 bla bla";
var matches = input.match(/-?\d+(?:\.\d+)?,\s*-?\d+(?:\.\d+)?/g);
console.log(matches);

Once you have isolated the lat/lng pairs in the above string array, it is only a bit more work to obtain separate numbers in whatever format you need.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading