-1

Regards,

I just want to ask that is there any method or class for Google Map Api key Version 2 with which i can fetch current location(in address) through latitude and longitude derived. In Google Map Api version 1 there was class Geocoder,which we can use to change the latitude and longitude to Address.But I think we can't use Geocoder class in Api v2 because it uses Fragment class. Anybody have solution ? Thank you.

Avin Sharma
  • 1
  • 1
  • 3

2 Answers2

0

Geocoder is not part of the Maps API v1 and you can use it with API v2.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • Do you have any example ? Or if you can show me some Code. Actually i did,But i got the latitude and longitude but i am not able to get the addresses of that particular location. – Avin Sharma May 18 '13 at 21:55
  • typing "geocoder example" in the search box on the top gives this: http://stackoverflow.com/questions/11430726/geocoding-example-not-working – MaciejGórski May 19 '13 at 07:45
  • Ok,Thank you. i will try this one. – Avin Sharma May 19 '13 at 15:01
0

yes the GeoCoder is really horrible. On some devices, that does not work correctly. You can do that, which is undependent... do a json-request:

IF YOU NEED THE OTHER WAY, THATS CODE WILL HELP YOU. ADRESS -> LOCATION

public static JSONObject getLocationInfo(String address) {

    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +address+"&ka&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}

public static GeoPoint getGeoPoint(JSONObject jsonObject) {

    Double lon = new Double(0);
    Double lat = new Double(0);

    try {

        lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");

        lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

}
Luser_k
  • 514
  • 1
  • 4
  • 18