gpt4 book ai didi

java - 如何从谷歌地图短网址中提取坐标?

转载 作者:行者123 更新时间:2023-12-05 03:34:49 26 4
gpt4 key购买 nike

我有一个谷歌地图短网址列表,例如:

https://goo.gl/maps/4hHGt2147ijXHkpC8

我想知道如何使用 api 或客户端库从每个短 url 中提取位置纬度和经度?

注意:用于获取长 url 的 URL Shortener API 不再起作用。

https://www.googleapis.com/urlshortener/v1/url?key=[my_api_key]&shortUrl=[my_short_url]

更新: 到目前为止我找到的所有答案都是针对我没有的长 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;

}

}

问题:得到如下长url后

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+SSANGYONG+Bazie%E2%80%AD/@24.695477,46.7345518,18z/data=!4m5!3m4!1s0x3e2f03f1077c5fbf:0x9003e7394e3408f8!8m2!3d24.6968319!4d46.7353356?hl=ar&shorturl=1

捕获的坐标并非 100% 准确因为当我在浏览器中打开它几秒钟后它重定向到一个我无法捕获的新 url,所以我试图为它获取重定向 url 并且它产生相同的结果。

这个问题是否有解决方案或 API 提取精确坐标的其他建议?

最佳答案

@24.695477,46.7345518 之后的坐标是 map 的中心,而不是位置,在您的 URL 中: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+SSANGYONG+Bazie%E2%80%AD/@24.695477,46.7345518,18z/data=!4m5!3m4!1s0x3e2f03f1077c5fbf:0x9003e7394e3408f8!8m2!3d24.6968319!4d46.7353356?hl=ar&shorturl=1

Google 在 map 查询中使用网络标准 key1=value1&key=value2 样式。这些是 Google map 查询中字符的含义:

d - double-precision floating-point

f - single-precision floating-point

i - integer

s - string

z - encoded data or an id or some kind

b - byte or boolean (?)

v - timestamp, Unix epoch in milliseconds

这些是您的链接在“data=”部分的参数组:

!4m5

!3m4

!1s0x3e2f03f1077c5fbf:0x9003e7394e3408f8

!8m2

!3d24.6968319

!4d46.7353356

?hl=ar&shorturl=1

有许多参数组可用于根据 XML 层次结构创建谷歌地图查询。

例如:

"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"

实际上,每个参数都代表 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>

最后,在您的 Java 代码中,您有一个检查 3d 和 4d 的模式,但它只会再次到达 map 的中心。

NOTE: Actually, you can also use the !1s parameter(Yours is that!1s0x3e2f03f1077c5fbf:0x9003e7394e3408f8).But it also redirects thesame long url. It has hexadecimal code as param1:param2. Convertparam2 from hexadecimal to decimal and pass it in cid ( CustomerId) parameter like this :https://google.com/maps?cid=10377392199702939896

你需要 !2z 参数来找到像这样的 base64 编码数据的真实坐标:

  • !2zMzfCsDQ3JzM0LjUiUyAxNDXCsDAwJzU2LjYiRQ

然后对其进行解码以获得真实坐标,但您的长 URL 中没有“z”字段。也许 Google 在 stopped supporting "goo.gl" short links 之后更改了查询标准2019 年 3 月 30 日。

请检查这些链接以获取更多信息:

Andrew Whitby

Pete Watson-Wailes

Blog

谢谢。

关于java - 如何从谷歌地图短网址中提取坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70054667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com