Abhishek Abhishek - 13 days ago 5x
Android Question

Getting coordinates of location shared via message from gmaps in android

I'm writing a function to get the coordinates of a location shared via message from maps app in android. I'm reading the shortened url from the message and following the redirection to get the new url. The new url shows the map with the shared location centred. However this url also doesn't have the coordinates in it. When the map loads from the second url, then without refreshing the page the final url loads with the coordinates in it. I've to read that url. For example:-

Shortened url: goo.gl/maps/LUZi5

Redirected url(permanent redirection): www.google.com/maps/preview?cid=808842597169779873&hl=en&gl=in

The map loads with this new url with the place of interest in centre. However this url does't have coordinates information of the place. After sometime the url changes without refreshing/ reloading the page.

Final url: google.com/maps/place/Radhika+Theatre/@15.140191,76.930298,17z/data=!3m1!4b1!4m2!3m1!1s0x0:0xb39960e838254a1?hl=en

Here we can see the coordinates of the place after @.

I want to capture this url in my app. After capturing the redirection, I'm only getting the second url and not the final url. How to do it. Please help

ps: It seems that the final url appears after the page is loaded completely.


I'm not an expert, but I have done some research and you can do some kind of workaround:

  1. Create a google analytics link like: "https://goo.gl/#analytics/" + shortenedUrl + "/all_time". For you it will be https://goo.gl/#analytics/goo.gl/maps/LUZi5/all_time

  2. Now, using a html parser you can easly obtain an url with cid:

    <div class="IXTYPID-h-g IXTYPID-b-a"> 
        <a href="http://maps.google.com/?cid=808842597169779873&amp;hl=en&amp;gl=in">http://maps.google.com/?cid=808842597169779873&amp;hl=en&amp;gl=in</a> 
              <span>2014 Jun 4</span>
  3. When you have cid number, you can use method from this thread: http://stackoverflow.com/a/24131148/3804906 to obtain coordinates.

UPDATE 2016-10-10

It seems like google changed behaviour of shortened links. It redirects twice now. First into link with with cid like http://www.google.com/maps/preview?cid=808842597169779873&hl=en&gl=in

Finally url change into https://www.google.com/maps/place/Radhika+Theatre/@15.140191,76.930298,17z/data=!3m1!4b1!4m2!3m1!1s0x0:0xb39960e838254a1?hl=en automatically