0
votes

I am trying get the lat lon value from the shortened google maps url, https://goo.gl/maps/81bgEw3JYMT2

I have tried with googleapi and get the following response, https://www.googleapis.com/urlshortener/v1/url?shortUrl=https://goo.gl/maps/81bgEw3JYMT2&key=USE_YOUR_KEY

{
 "kind": "urlshortener#url",
 "id": "https://goo.gl/maps/81bgEw3JYMT2",
 "longUrl": "https://maps.google.com/?q=Meenakshi+Amman+Temple,+Madurai,+Tamil+Nadu+625001&ftid=0x3b00c58461e46987:0xf134621ce5286703&hl=en-US&gl=us&shorturl=1",
 "status": "OK"
}

I have also tried checking with the redirection URL, and i get the following finalurl, https://maps.google.com/?q=Meenakshi+Amman+Temple,+Madurai,+Tamil+Nadu+625001&ftid=0x3b00c58461e46987:0xf134621ce5286703&hl=en-US&gl=us

Code for checking the redirection URL,

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0];
[request setHTTPMethod:@"HEAD"];
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSURL *finalURL = response.URL;

Is there another way to get the lat lon value?

Bounty: Accepted answer will get promocode for the app I'm working for. ;)

2
Hey @KingofBliss check this SO question, a user gave a sample code on how to open shortened url in his app. I hope it helps - Mr.Rebot
@Mr.Rebot I have already figured out how to expand the shortened url. But the issue is I'm not getting the lat lon value as mentioned in that answer. - KingofBliss

2 Answers

0
votes

It sounds like you've figured out how to expand the short URL from goo.gl into the full Google Maps URL. Looking at the sample you've provided, the q parameter isn't a latlng but is a string. When q doesn't look like a latlng (perhaps you can use a regex to check this) you should call Google's Places API or Geocoding API to lookup the place or address from that q value.

A request to the Places TextSearch API like: https://maps.googleapis.com/maps/api/place/textsearch/json?query=Meenakshi+Amman+Temple,+Madurai,+Tamil+Nadu+625001&key=<API KEY HERE> returns information about the temple with a latlng included you could use in your app.

0
votes

Here is the example to expand Maps Url

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new Runnable() {
        @Override
        public void run() {
            String expandedURL1=expand("GIVE YOUR SHORTNED URL");
            Log.d("Expanded", "run: "+expandedURL1);
        }
    }).start();

}

public static String expand(String url) {
    String s3 = "";
    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        int s = connection.getInputStream().read();
        URL s2 = connection.getURL();
        s3 = String.valueOf(s2);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return s3;
}

}

Output of the code is: D/Expanded: run: "IN LOG IT WILL PRINT EXPANDED URL"

"Make sure to give INTERNET permission"