1

I'm building an app which shows the user there current address by Toast, using lat long. I'm using Geocoder to convert lat long into address, but the case is, on some devices address is working fine but in some devices its crashes. Then I debug the app on those devices on it crashes, I found that after geocoder line it jumps to catch(Exception), without executing the other lines. So, is there any other ways to get location by lat long.

There is another question with same problem here ,But that did't give much solution so i'm asking it again.

My Geocoder block is

if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
        try {
            geocoder = new Geocoder(this, Locale.getDefault());
            addresses = geocoder.getFromLocation(latitude,longitude,1);
            if (addresses != null && addresses.size() > 0){
                String address = addresses.get(0).getAddressLine(0);
                String city = addresses.get(0).getLocality();
                String state = addresses.get(0).getAdminArea();
                String country = addresses.get(0).getCountryName();
                String postalCode = addresses.get(0).getPostalCode();
                Toast.makeText(getApplicationContext(), "Your address is: " +address+ " "+city+ " " + state+ "\n" +country+ " "+ postalCode, Toast.LENGTH_LONG).show();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
Neck
  • 611
  • 1
  • 7
  • 21
  • Refer this link https://stackoverflow.com/questions/45004239/is-there-any-way-to-get-city-name-for-google-place-picker-android/45004308?noredirect=1#comment76989519_45004308 – Yyy Jul 12 '17 at 07:09
  • Thanks @NancyY , But I think you used the same codes, However I'll give it a try. – Neck Jul 12 '17 at 07:21
  • In that link I have also added an alternate solution, which you can try – Yyy Jul 12 '17 at 07:25
  • Thanks @NancyY , It can work, gonna try it, thanks again.. – Neck Jul 12 '17 at 07:36

3 Answers3

2

You can simply hit google-maps web service passing the Latitude and longitude. It is simply a GET-Method web-service.

http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true

Replace lat and long with your own lat&long.

Praveen Rawat
  • 314
  • 1
  • 3
  • 14
  • The actual reason why Geocoder was not working is because the NetworkLocator was killed in action. Probably due to less memory or maybe you used the Task Manager to kill all services? – Praveen Rawat Jul 12 '17 at 07:33
0

You can use this Google API for get complete information related to Latitude and Longitude.

http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true

here 32 is latitude and 75 is longitude.

PankajSharma
  • 1,529
  • 14
  • 27
0

String city = getLocationCityName(lat,lang);

public static String getLocationCityName(double lat, double lon) {
    JSONObject result = getLocationFormGoogle(lat + "," + lon);
    String city = getCityAddress(result);

    return getCityAddress(result);
}

protected static JSONObject getLocationFormGoogle(String placesName) {

    String apiRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + placesName; //+ "&ka&sensor=false"
    HttpGet httpGet = new HttpGet(apiRequest);
    HttpClient client = new DefaultHttpClient();
    org.apache.http.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) {

        e.printStackTrace();
    }

    return jsonObject;
}


protected static String getCityAddress(JSONObject result) {
    if (result.has("results")) {
        try {
            JSONArray array = result.getJSONArray("results");
            if (array.length() > 0) {
                JSONObject place = array.getJSONObject(0);
                JSONArray components = place.getJSONArray("address_components");
                for (int i = 0; i < components.length(); i++) {
                    JSONObject component = components.getJSONObject(i);
                    JSONArray types = component.getJSONArray("types");
                    for (int j = 0; j < types.length(); j++) {
                        if (types.getString(j).equals("locality")) {//city
                            String city = component.getString("long_name");
                            Log.d("city", city);

                            return component.getString("long_name");
                        }

                        if (types.getString(j).equals("postal_code")) {//pin
                            return component.getString("long_name");
                        }

                        if (types.getString(j).equals("country")) {//country
                            return component.getString("long_name");
                        }

                        if (types.getString(j).equals("administrative_area_level_1")) {//state
                            return component.getString("long_name");
                        }

                        if (types.getString(j).equals("administrative_area_level_2")) {
                            return component.getString("long_name");
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return null;
}
Sandya
  • 1