Android get lat and long from address



You can use the Google Geocoder service in the Google Maps API to convert from your location name to a latitude and longitude. So you need some code like

The idea is simple. Make a request to Google Map server and it will return an JSON or XML. Then, parse the JSON to get Lat and Long.

 I have used Google Maps API Here.


    // JSON Node names
    private static final String TAG_RESULTS = "results";
    private static final String TAG_GEOMETRY = "geometry";
    private static final String TAG_VIEWPORT = "viewport";
    private static final String TAG_NORTHEAST = "northeast";
    private static final String TAG_LAT = "lat";
    private static final String TAG_LNG = "lng";
    // contacts JSONArray
    JSONArray results = null;

Android get lat and long from address

// Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        address = "Rajkot, Gujarat, India";

        address = address.replaceAll(" ", "%20");
        url = "http://maps.googleapis.com/maps/api/geocode/json?address="
                + address + "&sensor=true";

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of results
            results = json.getJSONArray(TAG_RESULTS);

            Toast.makeText(getApplication(),
                    "Number of results : " + results.length(),
                    Toast.LENGTH_LONG).show();

            for (int i = 0; i < results.length(); i++) {
                JSONObject r = results.getJSONObject(i);

                // geometry and location is again JSON Object
                JSONObject geometry = r.getJSONObject(TAG_GEOMETRY);

                JSONObject viewport = geometry.getJSONObject(TAG_VIEWPORT);

                JSONObject northest = viewport.getJSONObject(TAG_NORTHEAST);

                String lat = northest.getString(TAG_LAT);
                String lng = northest.getString(TAG_LNG);

                tvlat.setText(lat);
                tvlng.setText(lng);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


JSONParser.java

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}


 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



Android get lat and long from address Android get lat and long from address Reviewed by Nirav Ranpara on 11:23 PM Rating: 5

Random Posts

My Android Tuts. Powered by Blogger.