我有一个谷歌地图短网址列表,例如:
https://goo.gl/maps/4hHGt2147ijXHkpC8
我想知道如何使用 api 或客户端库从每个短网址中提取位置纬度和经度?
注意:用于获取长 url 的 URL Shortener API 不再起作用。
https://www.googleapis.com/urlshortener/v1/url?key=[my_api_key]&shortUrl=[my_short_url]
更新:到目前为止我找到的所有答案都是针对我没有的长网址,要获得它,我必须将链接放入浏览器中,并且我有大约 10000 个链接,那么除了开发一个抓取工具在浏览器中打开短网址以获取长网址,然后从长网址中提取坐标?
编辑:
我尝试了以下代码:
public class Test {
public static void main(String[] args) throws Exception {
String url = "https://goo.gl/maps/chcK1sV1BtpYirAP9";
String urlLong = getRedirectURL(url);
System.out.println(urlLong);
System.out.println("maps final url: " + buildMapURL(urlLong));
}
public static String buildMapURL(String url) {
Pattern pattern = Pattern.compile("!3d\\d+\\.\\d+!4d\\d+\\.\\d+");
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
String result = matcher.group(0).replace("!3d", "").replace("!4d", ",");
return "https://maps.google.com/maps?q=" + result;
}
return "";
}
public static String getRedirectURL(String strURL) throws Exception {
InputStream is = null;
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
URL url = new URL(strURL);
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setInstanceFollowRedirects(true);
HttpsURLConnection.setFollowRedirects(true);
https.connect();
int responseCode = https.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
is = https.getInputStream();
return https.getURL().toString();
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (is != null)
is.close();
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
问题:当我得到如下长网址后
捕获的坐标不是 100% 准确,因为当我在浏览器中打开它几秒钟后,它会重定向到一个我无法捕获的新网址,所以我尝试获取它的重定向网址,它会产生相同的结果。
是否有针对此问题的解决方案或 API 提取精确坐标的其他建议?
@ 24.695477,46.7345518 之后的坐标是地图的中心,而不是您的网址中的位置: https: //www.google.com.sa/maps/place/%D8%B3%D8%A7 %D9%86%D8%AC+%D9%8A%D9%88%D9%86%D8%AC+%D8%A7%D9%84%D8%A8%D8%A7%D8%B2%D8%B9%D9 %8A+双龙+巴齐%E2%80%AD/@24.695477,46.7345518,18z/data=!4m5!3m4!1s0x3e2f03f1077c5fbf:0x9003e7394e3408f8!8m2!3d24.6968319!4d46.735第3356章
key1=value1&key=value2Google在地图查询中使用网络标准样式。这些是 Google 地图查询中字符的含义:
d - 双精度浮点
f - 单精度浮点
i - 整数
s - 字符串
z - 编码数据或 id 或某种
b - 字节或布尔值 (?)
v - 时间戳,Unix 纪元(以毫秒为单位)
这些是“data=”部分链接的参数组:
!4米5
!3立方米
!1s0x3e2f03f1077c5fbf:0x9003e7394e3408f8
!8平方米
!3d24.6968319
!4d46.7353356
?hl=ar&短网址=1
有很多参数组可用于根据 XML 层次结构创建 google 地图查询。
例如 :
"https://www.google.com/maps/embed?pb=" +
"!1m18" +
"!1m12" +
"!1m3" +
"!1d3152.8774048836685" +
"!2d145.01352231578036" +
"!3d-37.792912740624445" +
"!2m3" +
"!1f0" +
"!2f0" +
"!3f0" +
"!3m2" +
"!1i1024" +
"!2i768" +
"!4f13.1" +
"!3m3" +
"!1m2" +
"!1s0x0%3A0x0" +
"!2zMzfCsDQ3JzM0LjUiUyAxNDXCsDAwJzU2LjYiRQ" +
"!5e0" +
"!3m2" +
"!1sen" +
"!2sau" +
"!4v1471218824160"
Run Code Online (Sandbox Code Playgroud)
实际上,每个参数都代表 XML 模式中的一个标签,如下例所示:
<kml>
<Response>
<name>London</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>London, Greater London, UK</address>
<AddressDetails Accuracy="4">
<Country>
<CountryNameCode>GB</CountryNameCode>
<AdministrativeArea>
<AdministrativeAreaName>England</AdministrativeAreaName>
<SubAdministrativeArea>
<SubAdministrativeAreaName>Greater London</SubAdministrativeAreaName>
<Locality>
<LocalityName>London</LocalityName>
</Locality>
</SubAdministrativeArea>
</AdministrativeArea>
</Country>
</AddressDetails>
<Point>
<coordinates>0.126236,51.500152,0</coordinates>
</Point>
</Placemark>
</Response>
</kml>
Run Code Online (Sandbox Code Playgroud)
最后,在您的 Java 代码中,您有一个检查3d 和 4d的模式,但它只会再次到达地图的中心。
注意:实际上,您也可以使用
!1s参数(您的就是那个!1s0x3e2f03f1077c5fbf:0x9003e7394e3408f8)。但它也会重定向相同的长网址。它的十六进制代码为param1:param2. 从十六进制转换param2为十进制并将其传递到cid( CustomerId )参数中,如下所示: https://google.com/maps ?cid=10377392199702939896
您需要!2z参数来查找具有 Base64 编码数据的真实坐标,如下所示:
然后对其进行解码以获取真实坐标,但长网址中没有“z”字段。也许谷歌 在2019年3月30日停止支持“goo.gl”短链接后改变了查询标准。
请检查这些链接以获取更多信息:
谢谢。