location-samples icon indicating copy to clipboard operation
location-samples copied to clipboard

Geocode for java

Open panjisadewo opened this issue 3 years ago • 1 comments

I try geocode but i has error like this. java.io.IOException: Service not Available 2022-10-18 12:30:10.785 4101-4101/? W/System.err: at android.location.Geocoder.getFromLocationName(Geocoder.java:178)

This is my code

    Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
    List<Address> addresses;;
    try {
        addresses = geocoder.getFromLocation(-6.193479061, 106.848579406, 10);
        if(addresses.size() > 0){
            String cityName = addresses.get(0).getCountryName();
            String stateName = addresses.get(0).getAddressLine(1);
            String countryName = addresses.get(0).getAddressLine(2);
            Toast.makeText(getActivity(), addresses.size(), Toast.LENGTH_LONG);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

panjisadewo avatar Oct 18 '22 05:10 panjisadewo

You need to check if the service is available or not on the device like this,

if (GeoCoder.isPresent()) {
    //TODO Get the address
}

SweetD3v avatar Jul 03 '23 04:07 SweetD3v