20

What i have: currently my app is only telling me the coordinates of my current location.

What i want: Get location name from coordinates fetched by gps, so that i could know where exactly i am. (Name of location)

Noman
  • 4,049
  • 10
  • 38
  • 59
  • 1
    are you looking for this '[given-a-latitude-and-longitude-get-the-location-name][1]' [1]: http://stackoverflow.com/questions/6172451/given-a-latitude-and-longitude-get-the-location-name – Samuel Aug 03 '11 at 06:14
  • Have a look at [How To Program Google Android](http://blogoscoped.com/archive/2008-12-15-n14.html) – Adil Soomro Aug 03 '11 at 06:15
  • 1
    Your question is same as the question I posted a few months back..check this link http://stackoverflow.com/questions/6172451/given-a-latitude-and-longitude-get-the-location-name or check my answer below... – Krishna Aug 03 '11 at 06:17
  • @Krishna yes i have seen it but the answers given to ur question were not dat much easy so i decided ti put mine. – Noman Aug 03 '11 at 06:29
  • @Sam Quest.. answers given there are not that much understood by me. – Noman Aug 03 '11 at 06:31
  • @Noman.. almost all of us have said the same thing. The Geocoder class in the googles map api kit gives location details like Place name, address, etc. – Samuel Aug 03 '11 at 06:50
  • **android.location.Geocoder.getFromLocation(latitude, longitude, maxResults)**, Returns an array of Addresses that are known to describe the area immediately surrounding the given latitude and longitude. – Samuel Aug 03 '11 at 06:54

3 Answers3

34

Here is complete code from fetching long - lat to getting address:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);

Location locations = locationManager.getLastKnownLocation(provider);
List<String>  providerList = locationManager.getAllProviders();
if(null!=locations && null!=providerList && providerList.size()>0){                 
double longitude = locations.getLongitude();
double latitude = locations.getLatitude();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());                 
try {
    List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
    if(null!=listAddresses&&listAddresses.size()>0){
        String _Location = listAddresses.get(0).getAddressLine(0);
    }
} catch (IOException e) {
    e.printStackTrace();
}

}
Nicklas A.
  • 6,501
  • 7
  • 40
  • 65
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
7

You can us the GeoCoder which is available in android.location.Geocoder package. The JavaDocs gives u full explaination. The possible sample for u.

 List<Address> list = geoCoder.getFromLocation(location
                .getLatitude(), location.getLongitude(), 1);
        if (list != null & list.size() > 0) {
            Address address = list.get(0);
            result = address.getLocality();
            return result;

The result will return the name of the location.

Hussain
  • 5,552
  • 4
  • 40
  • 50
6

Here i am given a single just pass the latitude and longitude in this function then you got all the information related to this latitude and longitude.

public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                + obj.getAdminArea();
        GUIStatics.latitude = obj.getLatitude();
        GUIStatics.longitude = obj.getLongitude();
        GUIStatics.currentCity= obj.getSubAdminArea();
        GUIStatics.currentState= obj.getAdminArea();
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,
        // Toast.LENGTH_SHORT).show();

        // TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

I hope you get the solution to your answer.

Krishna
  • 1,454
  • 5
  • 16
  • 31
  • 1
    how can i get the exact location of my place, it gives max the city value, i want the locaation name inside city – Apoorv Jun 09 '16 at 08:08