Skip to content Skip to sidebar Skip to footer

Get Latitude And Longitude From City Name

In my current android application, I would like to get the geocordinates based on an entered city name, street name or zip code. How can I accomplish this? Best Regards

Solution 1:

get geo co-ordinates via net

privatestaticJSONObjectgetLocationInfo(String address)
{

    StringBuilder stringBuilder = newStringBuilder();
    try {

    address = address.replaceAll(" ","%20");    

    HttpPost httppost = newHttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
    HttpClient client = newDefaultHttpClient();
    HttpResponse response;
    stringBuilder = newStringBuilder();


        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
        Log.i("getLocationInfo ClientProtocolException", e.toString());
    } catch (IOException e) {

        Log.i("getLocationInfo IOException", e.toString());
    }


    JSONObject jsonObject = newJSONObject();
    try {
        jsonObject = newJSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch blockLog.i("getLocationInfo JSONException", e.toString());
    }

    return jsonObject;
}

privatestaticbooleangetLatLong(JSONObject jsonObject) 
{

    try {

       longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
        Log.i("Log1", longitute1+"");
    latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");
        Log.i("lat1", latitude1+"");
    } catch (JSONException e) {

        longitute=0;
        latitude = 0;
        Log.i("getLatLong", e.toString());
        returnfalse;

    }

    returntrue;
}

Solution 2:

searchedAddress --> Can be (city name/address/Zipcode).

publicbooleangetLatitudeAndLongitudeFromGoogleMapForAddress(String searchedAddress){

    Geocodercoder=newGeocoder(IPlant.iPlantActivity);
    List<Address> address;
    try 
    {
        address = coder.getFromLocationName(searchedAddress,5);
        if (address == null) {
            Log.d(TAG, "############Address not correct #########");
        }
        Addresslocation= address.get(0);

        Log.d(TAG, "Address Latitude : "+ location.getLatitude();+ "Address Longitude : "+ location.getLongitude());
        returntrue;

    }
    catch(Exception e)
    {
        Log.d(TAG, "MY_ERROR : ############Address Not Found");
        returnfalse;
    }
}

Solution 3:

Use Geocoder class. It's not difficult to use, and then you can use getFromLocationName, which does what you want. You can also use this class the other way round, it's to say, entering (lat, lon) and getting the address located there.

I assume you want to do it through Android code, if you want to use Javascript, then you should use Google map's API, as pointed in another answer.

Post a Comment for "Get Latitude And Longitude From City Name"