Get location name from fetched coordinates

前端 未结 3 844
死守一世寂寞
死守一世寂寞 2020-12-02 18:02

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

What i want: Get location name from coordi

3条回答
  •  清歌不尽
    2020-12-02 18:15

    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  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
    listAddresses = geocoder.getFromLocation(latitude, longitude, 1); if(null!=listAddresses&&listAddresses.size()>0){ String _Location = listAddresses.get(0).getAddressLine(0); } } catch (IOException e) { e.printStackTrace(); } }

提交回复
热议问题