I have a list of google maps short urls like :
https://goo.gl/maps/4hHGt2147ijXHkpC8
and I was wondering how to extract location latitude and longitude from each short url with an api or client library ?
NOTE: The URL Shortener API to get the long url doesn't work anymore.
https://www.googleapis.com/urlshortener/v1/url?key=[my_api_key]&shortUrl=[my_short_url]
UPDATE: all the answers I have found so far was for the long url which I don't have, and to obtain it I have to put the link in the browser, and I have like 10000 link, so is there's any other solution than developing a scraping tool to open the short url in the browser to obtain the long url and then extract coordinates from the long url ?
EDIT:
i tried the following code :
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;
}
}
Issue: After i get the long url as follows
the captured coordinates is not 100% accurate since when I open it in browser after couple of seconds it redirects to a new url which I am not able to capture, so i tried to get the redirect url for it and it produces same result.
Is there's a solution for this issue or another suggestion for API to extract exact coordinates ?