从URL谷歌地图获取经度 [英] Get latitude-longitude from URL Google Maps

查看:95
本文介绍了从URL谷歌地图获取经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来计算出谷歌地图中某个地方的经度和纬度,并提供其链接.另外,如果可以的话,我可以在地图上绘制应用程序内的点,或者使用某种意图从Google地图共享纬度经度,这样我就可以在应用程序内接收该点.以上任何一种可能直接或间接吗?

I need a way to figure out the latitude, longitude of a place in google map provided its link. Alternatively, it is fine if I can plot the point inside my application on a map, if possible, or share the latitude longitude from google map using some intent, so that I can receive it from within my application. Is any of the above possible directly or indirectly?

简而言之,我随身带有一个谷歌地图URL(我可能已经在WhatsApp或类似平台中收到了),并且以某种方式使用它,即使我在应用程序中也需要获取该位置的纬度和经度,即使这是一种间接的方式.我对places API没兴趣,因为它有限制.

In short, I have a google map URL with me(which I may have received in WhatsApp or similar platform), and using that, somehow, I need to get the latitude and longitude of the place within my application, even if it is an indirect way. I am not interested in places API, since it having a limit.

已经尝试了几个小时的解决方案,没有发现任何相关内容.

Have been trying to get to a solution for a few hours, didn't find anything relevant.

我尝试使用的链接示例:

Samples of link I am trying to use:

  • https://goo[dot]gl/maps/fSQSTjDR32m
  • https://maps.google.com/?cid=14927999966769665575&hl=en&gl=gb

(由于SO不允许,因此无法使用URL缩写,请将[dot]替换为.)

(Not able to use URL shortners, since SO doesn't allow, replace [dot] with .)

推荐答案

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

Seems, there is no one common solution for all types of encoded URLs, but for described two you can use:

1)表示https://goo[dot]gl/maps/fSQSTjDR32m之类的网址-Google URL缩短器API 请求如下:

1) for URLs like https://goo[dot]gl/maps/fSQSTjDR32m - Google URL Shortener API with requests like:

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

NB!您需要在

NB! You need valid key for your Android app with enabled URL Shortener API on Developer Console

您可以使用HttpURLConnection获得响应

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

或使用用于Java的URL Shortener API客户端库.有关更多详细信息,请参见问题/ogre>食人魔和答案.

or use URL Shortener API Client Library for Java. For more details take a look at this question of Ogre and answers.

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

So, for your shortened URL you should get JSON response like:

{
 "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答案:

where longUrl tag contains URL with desired latitude-longitude. According to this answer of treebles:

这不是第一组坐标@ 10.1213253,76.3478427-即 地图视口的中心.点的坐标 兴趣来自他们的fid,并表示为 !3d10.1213237!4d76.348392其中,!3d参数表示纬度,!4d参数表示兴趣点的经度.

It is not the first set of coordinates@10.1213253,76.3478427 - that is the center of the Maps' viewport. The coordinates of the point of interest comes after their fid and is indicated by !3d10.1213237!4d76.348392 Where the !3d parameter indicated the latitude and the !4d parameter indicates the longitude of the point of interest.

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

And you can find latitude-longitude in longUrl by !3d and !4d prefixes or using regular expression.

2)表示 https://maps.google .com/?cid = 14927999966769665575& hl = zh-CN& gl = gb ,您可以使用

2) for URLs like https://maps.google.com/?cid=14927999966769665575&hl=en&gl=gb you can use Google Maps API and URL request like

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

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

其中<YOUR_CID> = 14927999966769665575<YOUR_APP_KEY>对于已启用Google Maps API的Android应用有效键(在开发者控制台上与URL Shortener API相同).有关更多详细信息,请参见此答案/leon>利昂. 您可以使用HttpURLConnection获得与第1点类似的响应:

where <YOUR_CID> = 14927999966769665575 and <YOUR_APP_KEY> valid key for your Android app with enabled Google Maps API (on Developer Console same as for URL Shortener API). Fro more details take a look at this answer of Leon. You can use HttpURLConnection to get response like for point 1:

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

,因此您应该获得带有标签geometry和子标签" location

and as the result you should get big JSON with tag geometry and "subtag" location

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

,您可以从中获得经度.

and you can get latitude-longitude from it.

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

For other types URL (not https://goo[dot]gl/maps/fSQSTjDR32m nor https://maps.google.com/?cid=14927999966769665575&hl=en&gl=gb) you should find other schemes for decoding and extracting latitude-longitude.

这篇关于从URL谷歌地图获取经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆