0

I am fairly new to programming. I need to extract coordinates from a link I get when recieving my data. The object I'm interested in looks like this:

 event: {
"type": "message",
"attachments": [{
    "facebookUrl": "https://www.bing.com/maps/default.aspx?v=2&pc=FACEBK&mid=8100&where1=52.217125%2C+5.959805&FORM=FBKPL1&mkt=en-US"
  }],
}

So, I'm interested in event.attachments[0].facebookUrl (is this the right notation to access it?). What is the right approach to get the coordinates lat: 52.217125 and lon: +5.959805 from this? Problem is that lon can either start with + or -. I have no idea how to do this using for example regex matching.

1
  • 1
    your notation event.attachments[0].facebookUrl is correct Commented May 22, 2016 at 17:25

2 Answers 2

1

You're looking for a url parsing library. From there you can get the querystring (everything past the ?), and feed that into a query string library which will give you the arguments.

I am not linking to them because finding good libraries is an important skill, and these are ubiquitous libraries.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried using a regex instead of using a library, but it doesn't work. /([+-]?\d*\.?\d*)%2c([+-]?\d*\.?\d*)\&/
Don't use a regex. The number of assumptions you are making about the structure of the URL is crazy. Are lat long always going to be in that order? What if something else like zoom level or elevation has a similar enough structure to get picked up? Don't waste your time making kludge code when the general problem has already been solved. If it was node it would be url.parse(facebookURL, true).query.where1.split(', ') to get an array of [lat, long]. nodejs.org/docs/latest/api/url.html
0

The following regex matching works for me: /([+-]?\d*\.?\d*)%2c([+-]?\d*\.?\d*)/i.

It gives me an array with 3 elements: [52.217125%2C+5.959805, 52.217125, +5.959805] So I just use arr[1] and arr[2].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.