How to get complete address from latitude and longitude?

前端 未结 21 2471
深忆病人
深忆病人 2020-11-22 09:07

I want to get following values from Latitude and Longitude in android

  1. Street Address
  2. City / State
  3. Zip
  4. Complete Address
<
21条回答
  •  再見小時候
    2020-11-22 09:33

    In onCreate()..

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);
    
        if (location == null) {
            Toast.makeText(getApplicationContext(), "GPS signal not found",
                    3000).show();
        }
        if (location != null) {
            Log.e("location", "location--" + location);
            Log.e("latitude at beginning",
                    "@@@@@@@@@@@@@@@" + location.getLatitude());
            onLocationChanged(location);
        }
    

    Write the code in onLocationChanged()

    @Override
    public void onLocationChanged(Location location) {
    
        Geocoder geocoder;
        List
    addresses; geocoder = new Geocoder(this, Locale.getDefault()); latitude = location.getLatitude(); longitude = location.getLongitude(); Log.e("latitude", "latitude--" + latitude); try { Log.e("latitude", "inside latitude--" + latitude); addresses = geocoder.getFromLocation(latitude, longitude, 1); if (addresses != null && addresses.size() > 0) { String address = addresses.get(0).getAddressLine(0); String city = addresses.get(0).getLocality(); String state = addresses.get(0).getAdminArea(); String country = addresses.get(0).getCountryName(); String postalCode = addresses.get(0).getPostalCode(); String knownName = addresses.get(0).getFeatureName(); locationTxt.setText(address + " " + city + " " + country); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

提交回复
热议问题