Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

java - 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 possible, or share the latitude longitude from google map using some intent, so that I can receive it from within my application. Is any of the above possible directly or indirectly?

In short, I have a google map URL with me(which I may have received in WhatsApp or similar platform), and using that, somehow, I need to get the latitude and longitude of the place within my application, even if it is an indirect way. I am not interested in places API, since it having a limit.

Have been trying to get to a solution for a few hours, didn't find anything relevant.

EDIT:

Samples of link I am trying to use:

(Not able to use URL shortners, since SO doesn't allow, replace [dot] with .)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

...
URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?shortUrl=...");
HttpURLConnection con = (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:

...
URL url = new URL("https://maps.googleapis.com/maps/api/place/details/json?cid=...");
HttpURLConnection con = (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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...