简体   繁体   English

给定带有位置“ loc”搜索的链接,即可使用Google Maps API从标记中提取纬度和经度

[英]Given a link with a location “loc” search extract latitude and longitude from marker using Google Maps API

Say I have a given a link to a map with a location marker , how can I find the latitude and longitude from that map marker using JavaScript and the Google API? 假设我有一个带位置标记地图链接,如何使用JavaScript和Google API从该地图标记中查找经度和纬度?

The trick is that I don't know the address. 诀窍是我不知道地址。 Is there a way to find out what the address on the page is elegantly, without having to use regular expressions on the HTML? 有没有一种方法可以找到页面上的精美地址,而不必在HTML上使用正则表达式?

Specifically, I want to extract the address and latitude/longitude out of the given marker. 具体来说,我想从给定标记中提取地址和纬度/经度。

That's 'easy'. 这很容易'。 Google Maps api has a geolocation object, that is the same used by google maps. Google Maps api有一个地理定位对象,与Google Maps使用的相同。 So you should use something like that: 因此,您应该使用类似的方法:

var geocodeLocation = 'String of the adress you''re looking for';

  var geocoder = new google.maps.ClientGeocoder();
  geocoder.getLatLng(geocodeLocation, function(point) {
    if (point) {
      //Do something with point - //You have point.x and point.y
    }
  });

instead of using the Google Maps API you can just use the geocoding web service directly and get a response in JSON or XML 无需使用Google Maps API,您可以直接使用地理编码Web服务并以JSONXML格式获取响应

Documentation here . 文档在这里

The problem of finding the address given the URL is actually way easier than I thought. 查找给定URL的地址的问题实际上比我想象的要容易得多。 In my case, the Google Maps URLs I am given are of the form: 就我而言,我获得的Google Maps URL的格式为:

https://maps.google.com/?q=loc:+ADDRESS+CITY+STATE+COUNTRY https://maps.google.com/?q=loc:+ADDRESS+CITY+STATE+COUNTRY

So, I quickly get the address from the URL directly. 因此,我很快直接从URL获取地址。
I don't know if that's the default for google maps URLS. 我不知道这是否是Google Maps URLS的默认设置。 If not, then things would be more difficult. 如果没有,那么事情将会更加困难。

From here, it's a simple use of the geocoder, of which there are plenty of examples on the Google Maps API page . 从这里开始,它可以简单地使用地址解析器,在Google Maps API页面上有很多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM