Getting longitude/latitude from a link in JavaScript

风流意气都作罢 提交于 2019-12-08 12:38:58

问题


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:


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.




回答2:


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



来源:https://stackoverflow.com/questions/37377199/getting-longitude-latitude-from-a-link-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!