0

I have tried using this code. It is not giving the location name. It is giving only latitude and longitude.

String latLongString;

if (location != null) 
{       
  double lat = location.getLatitude();    
  double lng = location.getLongitude();    
  latLongString = "Lat:" + lat + "\nLong:" + lng;    
   }
      else 
   {
       latLongString = "No location found"; 
   }

myLocationText.setText("Your Current Position is:\n" + latLongString);  
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
vinodkumar
  • 1
  • 1
  • 7

2 Answers2

2

Location by default means Co-ordinates which is latitude and longitude and that is what you are getting. To get the actual Address you need to geocode the coordinates.

There are 2 steps to be follow to get current location name.

1) To get current lovation that you have done,

now

2) Using the Geocoder class to convert that lat long into Address

See this answer for links and detail expression.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • 1
    Just expanding the above answer. Location by default means Co-ordinates which is latitude and longitude and that is what you are getting. To get the actual Address you need to geocode the coordinates. – PravinCG Feb 15 '12 at 17:01
  • But to use geocorder class to get location name we need internet connection. But isn't there a way to get the location name without connecting to internet? – Samitha Chathuranga May 06 '15 at 14:39
-1

Try this:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
    lat = location.getLatitude();
    lng = location.getLongitude();
} else {
    location = new Location("");
    location.setLatitude((double) 38.0000000000);
    location.setLongitude((double) -97.0000000000);
    lat = location.getLatitude();
    lng = location.getLongitude();
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Nitesh Khosla
  • 875
  • 8
  • 20