Skip to content Skip to sidebar Skip to footer

Get Latitude-longitude From Url Google Maps

I need a way to figure out the latitude, longitude of a place in google map provided its link. Alternatively, it is fine if I can plot the point inside my application on a map, if

Solution 1:

Seems, there is no one common solution for all types of encoded URLs, but for described two you can use:

1) for URLs like https://goo[dot]gl/maps/fSQSTjDR32m - Google URL Shortener API with requests like:

https://www.googleapis.com/urlshortener/v1/url?shortUrl=<YOUR_SHORT_URL_e_g_https://goo[dot]gl/maps/fSQSTjDR32m>&key=<YOUR_APP_KEY>

NB! You need valid key for your Android app with enabled URL Shortener API on Developer Console

You can use HttpURLConnection to get response

...
URLurl=newURL("https://www.googleapis.com/urlshortener/v1/url?shortUrl=...");
HttpURLConnectioncon= (HttpURLConnection) url.openConnection();
...

or use URL Shortener API Client Library for Java. For more details take a look at this question of Ogre and answers.

So, for your shortened URL you should get JSON response like:

{
 "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 tag contains URL with desired latitude-longitude. According to this answer of treebles:

It is not the first set of coordinates@10.1213253,76.3478427 - that is the center of the Maps' viewport. The coordinates of the point of interest comes after their fid and is indicated by !3d10.1213237!4d76.348392 Where the !3d parameter indicated the latitude and the !4d parameter indicates the longitude of the point of interest.

And you can find latitude-longitude in longUrl by !3d and !4d prefixes or using regular expression.

2) for URLs like https://maps.google.com/?cid=14927999966769665575&hl=en&gl=gb you can use Google Maps API and URL request like

https://maps.googleapis.com/maps/api/place/details/json?cid=&key=

where <YOUR_CID> = 14927999966769665575 and <YOUR_APP_KEY> valid key for your Android app with enabled Google Maps API (on Developer Console same as for URL Shortener API). Fro more details take a look at this answer of Leon. You can use HttpURLConnection to get response like for point 1:

...
URLurl=newURL("https://maps.googleapis.com/maps/api/place/details/json?cid=...");
HttpURLConnectioncon= (HttpURLConnection) url.openConnection();
...

and as the result you should get big JSON with tag geometry and "subtag" location

...
"geometry" : {
     "location" : {
        "lat" : 10.1652839,
        "lng" : 76.218744
     },
     "viewport" : {
        "northeast" : {
           "lat" : 10.1666328802915,
           "lng" : 76.22009298029151
        },
        "southwest" : {
           "lat" : 10.1639349197085,
           "lng" : 76.21739501970849
        }
     }
  },
...

and you can get latitude-longitude from it.

For other types URL (not https://goo[dot]gl/maps/fSQSTjDR32m nor https://maps.google.com/?cid=14927999966769665575&hl=en&gl=gb) you should find other schemes for decoding and extracting latitude-longitude.

Solution 2:

You can get latitude-longitude from URL Google Maps,

with using Regex Matcher and Pattern

Note:This works fine without a short link, like this :

https://www.google.com/maps/place/Egypt/@26.8444558,26.3722095,6z/data=!3m1!4b1!4m13!1m7!3m6!1s0x0:0x0!2zMTfCsDMwJzA0LjQiTiA3NMKwMDcnMjQuNyJF!3b1!8m2!3d17.5012212!4d74.1235132!3m4!1s0x14368976c35c36e9:0x2c45a00925c4c444!8m2!3d27.0982539!4d29.8828125

String url ="www.google.com/maps/place/@bla,bla"; // full linkPattern pattern =Pattern.compile("\\d+\\.\\d+,\\d+\\.\\d+");
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
        System.out.println(matcher.group(0));
    }

Now you will get latitude-longitude, I`f you want to split as two variable use this

if (matcher.find()) {
            String s = matcher.group(0);

            String lon = s.split(",")[0];
            String lat = s.split(",")[1];

            System.out.println(lon);
            System.out.println(lat);

        }

Post a Comment for "Get Latitude-longitude From Url Google Maps"