似乎,对于所有类型的编码 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=
其中<YOUR_CID> = 14927999966769665575 和<YOUR_APP_KEY> 是启用了 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),您应该找到其他方案来解码和提取经纬度。