Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

android - Get location name from fetched coordinates

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...