Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android convert CID location to Coordinates

I built an android app which can handle a share intent from Google Maps and show it's coordinates.

The problem is that they send a short url which I decode with Google's url shortner api and in some cases, the result long link is of this type: http://maps.google.com/?cid=3635533832900933072&hl=en&gl=us.

Can anyone help me on how to get the coresponding coordinates to "cid=3635533832900933072"

like image 927
Adrian Avatar asked May 31 '14 10:05

Adrian


People also ask

Can I put GPS coordinates into Google Maps app?

Enter Coordinates on Android If you copy the coordinates from another spot, you can easily paste them into Google Maps. But of course, you can type them too. Open Google Maps, enter the coordinates into the Search box at the top, and hit the Search key on the keyboard. You'll see a pin appear on the map for the spot.

How do I find coordinates for an app?

Google Maps For Android users, Google Maps is the simplest way to either enter latitude and longitude coordinates or to find out your current coordinates. If you want to find your current coordinates, zoom into your location and hold down on the touchpad screen in the map.

How do I find the geocode of a location?

One way to find your geolocation is to simply perform a lookup online. Melissa's Geocoder Lookup tool returns latitude, longitude, county, census tract and block data based on an address or ZIP Code. Simply enter the address or ZIP Code into the easy-to-use Lookups interface and submit.


2 Answers

As far as I know there is no public API to get the location from a cid.

However, a possible workaround would be to parse the Google Maps output to obtain the latitude and longitude (though it may be brittle, if they change the result format).

(Although the url contains output=json, it's not actually json -- that's why I parse it with substring() and such instead of using JSONObject).

Try this code:

public static LatLng getCidCoordinates(String cid)
{
    final String URL_FORMAT = "http://maps.google.com/maps?cid=%s&q=a&output=json"; 
    final String LATLNG_BEFORE = "viewport:{center:{";
    final String LATLNG_AFTER = "}";
    final String LATLNG_SEPARATOR = ",";
    final String LAT_PREFIX = "lat:";
    final String LNG_PREFIX = "lng:";

    try
    {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(String.format(URL_FORMAT, cid));
        HttpResponse response = client.execute(get);

        String text = EntityUtils.toString(response.getEntity(), "UTF-8");

        int startIndex = text.indexOf(LATLNG_BEFORE);
        if (startIndex == -1)
            return null;

        startIndex += LATLNG_BEFORE.length();
        int endIndex = text.indexOf(LATLNG_AFTER, startIndex);

        // Should be "lat:<number>,lng:<number>"
        String[] parts = text.substring(startIndex, endIndex).split(LATLNG_SEPARATOR);
        if (parts.length != 2)
            return null;

        if (parts[0].startsWith(LAT_PREFIX))
            parts[0] = parts[0].substring(LAT_PREFIX.length());
        else
            return null;

        if (parts[1].startsWith(LNG_PREFIX))
            parts[1] = parts[1].substring(LNG_PREFIX.length());
        else
            return null;

        return new LatLng(Double.parseDouble(parts[0]), Double.parseDouble(parts[1]));
    }
    catch (NumberFormatException e)
    {
        e.printStackTrace();
        return null;
    }
    catch (IOException e)
    {
        e.printStackTrace();
        return null;
    }
}
like image 173
matiash Avatar answered Oct 06 '22 20:10

matiash


After reading this post yesterday, I found a new method to do it. I hope Google do not close this new API and hidden parameter. :)

You can use this API hidden parameter to get the coordinater. Usage: https://maps.googleapis.com/maps/api/place/details/json?cid=YOUR_CID&key=YOUR_KEY

It returns a result contains formatted address, place_id, name of the address and GPS coordinater.

Please see my blog to see more detail: https://leonbbs.blogspot.com/2018/03/google-map-cid-to-placeid-or-get.html

like image 42
Leon Avatar answered Oct 06 '22 19:10

Leon