Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert GPS coordinates to locality

Tags:

android

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.

like image 742
Ahmed Avatar asked Aug 24 '12 02:08

Ahmed


2 Answers

Try this:

//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(); 
}
like image 147
ductran Avatar answered Oct 04 '22 05:10

ductran


The process of converting a point location (latitude, longitude) to a readable address or place name is called Reverse GeoCoding. [from Wikepedia]

You have to make use of GeoCoder class and use method getFromLocation. This method returns List<Address>, which you can access by iterating each Address object from the list.

Examples:

  1. http://www.edumobile.org/android/android-development/gecoding-example/
  2. Android: Reverse geocoding - getFromLocation
like image 22
Kartik Domadiya Avatar answered Oct 04 '22 06:10

Kartik Domadiya