I am trying to get the location from google map shared location link so I am using Google Shorten URL API to expand the URL but when I shared URL from Google Map application from the android device it gives me https://maps.app.goo.gl/eEhh3
this kind of URL. It does not give me actual expanded URL to provide Location information.
how to expand this:
https://maps.app.goo.gl/eEhh3
In this link:
https://www.google.com/maps/place/Siddique+Trade+Center/@31.5313297,74.3504459,17z/data=!3m1!4b1!4m5!3m4!1s0x391904e4af9b1e35:0xee6f3c848c9e5341!8m2!3d31.5313297!4d74.3526346
Here is the API URL which I am using to expand it.
"https://www.googleapis.com/urlshortener/v1/url?shortUrl=https://maps.app.goo.gl/eEhh3&key=(API_KEY)
"
But instead of giving me the above location information URL it returns me this Long URL.
https://maps.app.goo.gl/?link=https://www.google.com/maps/place//data%3D!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source%3Dmstt_1&apn=com.google.android.apps.maps&ibi=com.google.Maps&ius=comgooglemapsurl&isi=585027354
Please help me how can I get above-mentioned Location Info URL by Google API to get Location (Lat, Lng) through Google Map shared location Url from any device (IOS, WebAPP, Specifically Android).
Seems now (in 2019) there is no separate API or service for expanding shared location URL from Google Maps, but you can use workaround based on that trick: Google server can do it via Google Maps web interface.
TLDR;
You don't need expand API. For example, if
https://maps.app.goo.gl/eEhh3
URL typed in the web browser on PC (e.g. FireFox) than that URL was expanded and the marker was shown. And during that you can see in address field "partially expanded" URL like:
https://www.google.com/maps/place//data=!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source=mstt_1
which in 1-2 sec changed (redirected to) by "full expanded" URL with lat/lng coordinates like:
https://www.google.com/maps/place/Siddique+Trade+Center/@31.5313297,74.3504459,17z/data=!3m1!4b1!4m5!3m4!1s0x391904e4af9b1e35:0xee6f3c848c9e5341!8m2!3d31.5313297!4d74.3526346
.So you can use
WebView
to do the same and get "full expanded" from it (e.g. inpublic void onPageFinished(WebView view, String url)
ofWebViewClient
). NB: it's necessary to skip first "partially expanded" URL.The other issue:
WebView
converts shortened URL likehttps://maps.app.goo.gl/eEhh3
into Intent for Google Maps Application and "partially expanded" URLhttps://www.google.com/maps/place//data=!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source=mstt_1
never be loaded. Actually it's enough if you need just show marker. But if you need coordinates this issue can be solved if you open shortened URLhttps://maps.app.goo.gl/eEhh3
byHttpUrlConnection
and get redirected "partially expanded" URL viaHttpUrlConnection.getURL()
or viaLocation
Header field after connection opened andHttpUrlConnection.getInputStream()
called. Actually, if you load all response fromHttpUrlConnection
input stream asString
- you can find desired lat/lng coordinates within it byhttps://www.google.com/maps/preview/place
tag. Something like:... [[[1,42]\n]\n,0,null,0,47]\n]\n,null,\"Suit 706 Siddiq Trade Center, Main Blvd Gulberg, Block H Gulberg III, Lahore, Punjab, Pakistan\",null,null,\"https://www.google.com/maps/preview/place/Siddique+Trade+Center,+Suit+706+Siddiq+Trade+Center,+Main+Blvd+Gulberg,+Block+H+Gulberg+III,+Lahore,+Punjab,+Pakistan/@31.5313297,74.3526346,3401a,13.1y/data\\u003d!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341\",1,null,null,null,null,null, ...
for your shortened URL opened viaHttpUrlConnection
. IMHO better (but slightly slower) way is to pass "partially expanded" URL received fromHttpUrlConnection.getURL()
intoWebView
and get "full expanded" URL from its address line. So, with full source code:and
activity_main.xml
:you can get something like this:
NB! Described behavior can be changed by Google at any time.