2012-08-24 21 views
7

Sto programmando un'applicazione Android per convertire le coordinate di latitudine e logitudine dinamicamente disponibili in una posizione leggibile.Come convertire le coordinate GPS in località

Ad esempio, 12.2, 4.5 si trova nel centro di Londra, nel Regno Unito. Per quanto riguarda la granularità, voglio essere in grado di individuare la città-> città. O se no, almeno la città,

Qualcuno può consigliare su quali soluzioni sono disponibili per questo problema.

+0

Hai due risposte corrette. Segna la risposta come corretta che ti ha aiutato. –

risposta

9

Prova questa:

//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(); 
} 
2

Il processo di conversione di una posizione punto (latitudine, longitudine) per un indirizzo o luogo nome leggibile è chiamato Reverse GeoCoding. [da Wikepedia]

È necessario utilizzare la classe GeoCoder e utilizzare il metodo getFromLocation. Questo metodo restituisce List<Address>, a cui è possibile accedere ripetendo ogni oggetto Address dall'elenco.

Esempi:

  1. http://www.edumobile.org/android/android-development/gecoding-example/
  2. Android: Reverse geocoding - getFromLocation
0

penso che questo darà un risultato migliore:

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; 

} 
Problemi correlati