vinodkumar
vinodkumar

Reputation: 1

Get Current Location Name using Network Provider

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);  

Upvotes: 0

Views: 2429

Answers (2)

MKJParekh
MKJParekh

Reputation: 34301

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.

Upvotes: 2

Nitesh Khosla
Nitesh Khosla

Reputation: 875

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

Upvotes: -1

Related Questions