【问题标题】:Get latitude-longitude from URL Google Maps从 URL Google Maps 获取经纬度
【发布时间】:2018-08-18 09:45:55
【问题描述】:

我需要一种方法来计算谷歌地图中某个地方的纬度,经度提供了它的链接。或者,如果我可以在地图上绘制我的应用程序内的点,或者使用某种意图从谷歌地图共享纬度经度,这样我就可以从我的应用程序中接收它。以上是否有可能直接或间接?

简而言之,我有一个谷歌地图 URL(我可能在 WhatsApp 或类似平台中收到),并且使用它,不知何故,我需要在我的应用程序中获取该地点的纬度和经度,即使这是一种间接的方式。我对位置 API 不感兴趣,因为它有限制。

已经尝试了几个小时的解决方案,但没有找到任何相关信息。

编辑:

我尝试使用的链接示例:

(不能使用 URL 缩短器,因为 SO 不允许,请将 [dot] 替换为 .)

【问题讨论】:

  • 请举例说明这种链接。用它开始你的帖子。
  • 更新您的问题并添加正确的 url 示例
  • 请检查编辑。

标签: java android google-maps google-maps-android-api-2 google-url-shortener


【解决方案1】:

似乎,对于所有类型的编码 URL,没有一种通用的解决方案,但对于描述的两种,您可以使用:

1) 对于像 https://goo[dot]gl/maps/fSQSTjDR32m 这样的 URL - Google URL Shortener API 的请求如下:

https://www.googleapis.com/urlshortener/v1/url?shortUrl=<YOUR_SHORT_URL_e_g_https://goo[dot]gl/maps/fSQSTjDR32m>&key=<YOUR_APP_KEY>

注意!您需要在 Developer Console 上启用 URL Shortener API 的 Android 应用的有效密钥

您可以使用HttpURLConnection 获得回复

...
URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?shortUrl=...");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
...

或使用URL Shortener API Client Library for Java。有关更多详细信息,请查看thisOgre 的问题和答案。

因此,对于您的缩短 URL,您应该得到如下 JSON 响应:

{
 "kind": "urlshortener#url",
 "id": "https://goo<.>gl/maps/fSQSTjDR32m",
 "longUrl": "https://www.google.co.in/maps/place/10%C2%B007'16.8%22N+76%C2%B020'54.2%22E/@10.1213253,76.3478427,247m/data=!3m2!1e3!4b1!4m6!3m5!1s0x0:0x0!7e2!8m2!3d10.1213237!4d76.348392?shorturl=1",
 "status": "OK"
}

其中longUrl 标记包含具有所需纬度-经度的URL。根据treebles的this回答:

这不是第一组坐标@10.1213253,76.3478427——即 地图视口的中心。点的坐标 兴趣出现在他们的 fid 之后,并由 !3d10.1213237!4d76.348392 其中!3d 参数表示纬度,!4d 参数表示兴趣点的经度。

您可以通过!3d和!4d前缀或使用正则表达式在longUrl中找到经纬度。

2) 对于像 https://maps.google.com/?cid=14927999966769665575&hl=en&gl=gb 这样的 URL,您可以使用 Google Maps API 和像这样的 URL 请求

https://maps.googleapis.com/maps/api/place/details/json?cid=&key=

其中&lt;YOUR_CID&gt; = 14927999966769665575 和&lt;YOUR_APP_KEY&gt; 是启用了 Google Maps API 的 Android 应用的有效密钥(在开发者控制台上与 URL Shortener API 相同)。更多细节请看thisLeon 的答案。 您可以使用HttpURLConnection 来获得第 1 点的响应:

...
URL url = new URL("https://maps.googleapis.com/maps/api/place/details/json?cid=...");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
...

结果你应该得到带有标签geometry和“子标签”location的大JSON

...
"geometry" : {
     "location" : {
        "lat" : 10.1652839,
        "lng" : 76.218744
     },
     "viewport" : {
        "northeast" : {
           "lat" : 10.1666328802915,
           "lng" : 76.22009298029151
        },
        "southwest" : {
           "lat" : 10.1639349197085,
           "lng" : 76.21739501970849
        }
     }
  },
...

你可以从中得到经纬度。

对于其他类型的 URL(不是 https://goo[dot]gl/maps/fSQSTjDR32m 或 https://maps.google.com/?cid=14927999966769665575&hl=en&gl=gb),您应该找到其他方案来解码和提取经纬度。

【讨论】:

    【解决方案2】:

    您可以从 URL 谷歌地图获取经纬度,

    使用正则表达式匹配器和模式

    注意: 没有短链接也可以正常工作,如下所示:

    https://www.google.com/maps/place/Egypt/@26.8444558,26.3722095,6z/data=!3m1!4b1!4m13!1m7!3m6!1s0x0:0x0!2zMTfCsDMwJzA0LjQiTiA3NMKwMDcnMjQuNyJF!3b1!8m2!3d17.5012212!4d74.1235132!3m4!1s0x14368976c35c36e9:0x2c45a00925c4c444!8m2!3d27.0982539!4d29.8828125

    String url = "www.google.com/maps/place/@bla,bla"; // full link
    Pattern pattern = Pattern.compile("\\d+\\.\\d+,\\d+\\.\\d+");
    Matcher matcher = pattern.matcher(url);
    if (matcher.find()) {
            System.out.println(matcher.group(0));
        }
    

    现在你会得到纬度-经度,如果你想分成两个变量使用这个

    if (matcher.find()) {
                String s = matcher.group(0);
    
                String lon = s.split(",")[0];
                String lat = s.split(",")[1];
    
                System.out.println(lon);
                System.out.println(lat);
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多