似乎没有一种适用于所有类型的编码 URL 的通用解决方案,但对于所描述的两种,您可以使用:
1) 对于像这样的 URLhttps://goo[dot]gl/maps/fSQSTjDR32m
- 谷歌网址缩短 API请求如下:
https://www.googleapis.com/urlshortener/v1/url?shortUrl=<YOUR_SHORT_URL_e_g_https://goo[dot]gl/maps/fSQSTjDR32m>&key=<YOUR_APP_KEY>
注意!您的 Android 应用需要启用 URL Shortener API 的有效密钥开发者控制台
您可以使用HttpURLConnection
得到回应
...
URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?shortUrl=...");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
...
or use 适用于 Java 的 URL 缩短器 API 客户端库。欲了解更多详细信息,请查看this的问题Ogre和答案。
因此,对于缩短的 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"
}
where longUrl
标签包含具有所需经纬度的 URL。根据this小树的回答:
这不是第一组[电子邮件受保护],76.3478427 - 即
地图视口的中心。点的坐标
兴趣出现在他们的 fid 之后,并表示为
!3d10.1213237!4d76.348392 其中!3d 参数表示兴趣点的纬度,!4d 参数表示兴趣点的经度。
你可以找到经纬度longUrl
by !3d
and !4d
前缀或使用正则表达式。
2) 对于像这样的 URL您可以使用谷歌地图 API和 URL 请求类似
https://maps.googleapis.com/maps/api/place/details/json?cid=&键=
where <YOUR_CID> = 14927999966769665575
and <YOUR_APP_KEY>
启用 Google Maps API 的 Android 应用程序的有效密钥(在开发者控制台上与 URL Shortener API 相同)。更多详细信息请查看this的答案Leon。
您可以使用HttpURLConnection
获得与第 1 点类似的响应:
...
URL url = new URL("https://maps.googleapis.com/maps/api/place/details/json?cid=...");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
...
结果你应该得到带有标签的大 JSONgeometry
和“子标签”location
...
"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也不)你应该找到其他解码和提取经纬度的方案。