Cómo convertir coordenadas GPS a localidad

I am programming an Android app to convert the dynamically available latitude and logitude coordinates to a humanly readable location.

For example, 12.2, 4.5 is located in Central London, UK. Regarding the granularity I want to be able to atleast locate the city->town. Or if not, at least the city,

Can someone please advise on what solutions are available for this problem.

preguntado el 24 de agosto de 12 a las 03:08

You have two correct answers. Mark the answer as correct that helped you. -

3 Respuestas

Prueba esto:

//listenner location changed
private class MyLocListener implements LocationListener {
   public void onLocationChanged(Location location) {
      if (location != null) {
         Log.d("LOCATION CHANGED", location.getLatitude() + "");
         Log.d("LOCATION CHANGED", location.getLongitude() + "");
      }
   }
}

 //Get address base on location
try{
 Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
 List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
  if (addresses.isEmpty()) {
        yourtextfieldname.setText("Waiting for Location");
  }
  else {
     if (addresses.size() > 0) {       
        Log.d(TAG,addresses.get(0).getFeatureName() + ", 
         " + addresses.get(0).getLocality() +", 
         " + addresses.get(0).getAdminArea() + ",
         " + addresses.get(0).getCountryName());

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

Respondido 24 ago 12, 04:08

The process of converting a point location (latitude, longitude) to a readable address or place name is called Codificación geográfica inversa. [from Wikepedia]

Tienes que hacer uso de Geocodificador class and use method getFromLocation. Este método devuelve List<Address>, which you can access by iterating each Dirección objeto de la lista.

Ejemplos:

  1. http://www.edumobile.org/android/android-development/gecoding-example/
  2. Android: codificación geográfica inversa - getFromLocation

contestado el 23 de mayo de 17 a las 12:05

I think this will give a better result:

 private String convertLocationToAddress(Location location) {
    String addressText;
    String errorMessage = "";

    Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());

    List<Address> addresses = null;

    try {
        addresses = geocoder.getFromLocation(
                location.getLatitude(),
                location.getLongitude(),
                1
        );
    } catch (IOException ioException) {
        // Network or other I/O issues
        errorMessage = getString(R.string.network_service_error);
        Log.e(TAG, errorMessage, ioException);
    } catch (IllegalArgumentException illegalArgumentException) {
        // Invalid long / lat
        errorMessage = getString(R.string.invalid_long_lat);
        Log.e(TAG, errorMessage + ". " +
                "Latitude = " + location.getLatitude() +
                ", Longitude = " +
                location.getLongitude(), illegalArgumentException);
    }

    // No address was found
    if (addresses == null || addresses.size() == 0) {
        if (errorMessage.isEmpty()) {
            errorMessage = getString(R.string.no_address_found);
            Log.e(TAG, errorMessage);
        }
        addressText = errorMessage;

    } else {
        Address address = addresses.get(0);
        ArrayList<String> addressFragments = new ArrayList<>();

        // Fetch the address lines, join them, and return to thread
        for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
            addressFragments.add(address.getAddressLine(i));
        }
        Log.i(TAG, getString(R.string.address_found));
        addressText =
                TextUtils.join(System.getProperty("line.separator"),
                        addressFragments);
    }

    return addressText;

}

Respondido el 15 de Septiembre de 17 a las 15:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.