How to get complete address from latitude and longitude?

前端 未结 21 2125
深忆病人
深忆病人 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<Address> 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();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:36

    Use this it work for me :D

    Retrieve json data of the latitude and longitude.

    https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyAr29XeWWAeWZcrOgjjfs3iSnqkWtAz4No&latlng=2.1812,102.4266&sensor=true

    Change latitude , longitude with your own place.

    https://maps.googleapis.com/maps/api/geocode/json?key=<\API_KEY_HERE>&latlng="latitude","longitude"&sensor=true

    You can change the <\API_KEY_HERE> with your own key.

    Need to enable api service in google console for new api key.

    Hope it helps :D

    0 讨论(0)
  • 2020-11-22 09:37
    public String getAddress(LatLng latLng) {
        String cAddress = "";
        if (latLng == null) {
            errorMessage = "no_location_data_provided";
            Log.wtf(TAG, errorMessage);
            return "";
        }
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    
        // Address found using the Geocoder.
        List<Address> addresses = null;
    
        try {
            // Using getFromLocation() returns an array of Addresses for the area immediately
            // surrounding the given latitude and longitude. The results are a best guess and are
            // not guaranteed to be accurate.
            addresses = geocoder.getFromLocation(
                    latLng.latitude,
                    latLng.longitude,
                    // In this sample, we get just a single address.
                    1);
        } catch (IOException ioException) {
            // Catch network or other I/O problems.
            errorMessage = "service_not_available";
            Log.e(TAG, errorMessage, ioException);
        } catch (IllegalArgumentException illegalArgumentException) {
            // Catch invalid latitude or longitude values.
            errorMessage = "invalid_lat_long_used";
            Log.e(TAG, errorMessage + ". " +
                    "Latitude = " + latLng.latitude +
                    ", Longitude = " + latLng.longitude, illegalArgumentException);
        }
    
        // Handle case where no address was found.
        if (addresses == null || addresses.size() == 0) {
            if (errorMessage.isEmpty()) {
                errorMessage = "no_address_found";
                Log.e(TAG, errorMessage);
            }
        } else {
            Address address = addresses.get(0);
            ArrayList<String> addressFragments = new ArrayList<String>();
            // Fetch the address lines using {@code getAddressLine},
            // join them, and send them to the thread. The {@link android.location.address}
            // class provides other options for fetching address details that you may prefer
            // to use. Here are some examples:
            // getLocality() ("Mountain View", for example)
            // getAdminArea() ("CA", for example)
            // getPostalCode() ("94043", for example)
            // getCountryCode() ("US", for example)
            // getCountryName() ("United States", for example)
            String allAddress = "";
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                addressFragments.add(address.getAddressLine(i));
                allAddress += address.getAddressLine(i) + " ";
            }
            if (address.getAdminArea() != null) {
                state = address.getAdminArea();
            } else {
                state = "";
            }
            if (address.getLocality() != null) {
                city = address.getLocality();
            } else {
                city = "";
            }
            if (address.getPostalCode() != null) {
                postalCode = address.getPostalCode();
            } else {
                postalCode = "";
            }
    
            Log.i(TAG, "address_found");
            //driverAddress = TextUtils.join(System.getProperty("line.separator"), addressFragments);
            cAddress = allAddress;
            Log.e("result", cAddress.toString());
        }
        return cAddress;
    }
    

    You Can use this method for geocoding proper complete Address

    0 讨论(0)
  • 2020-11-22 09:38
      public static String getAddressFromLatLng(Context context, LatLng latLng) {
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(context, Locale.getDefault());
        try {
            addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
            return addresses.get(0).getAddressLine(0);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:39

    City & Country are not Always getting in address Line 1 & Line 2...

    Example is here

    So,

    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    
    List<Address> addresses  = geocoder.getFromLocation(latitude,longitude, 1);
    String address = addresses.get(0).getAddressLine(0);
    String city = addresses.get(0).getLocality();
    String state = addresses.get(0).getAdminArea();
    String zip = addresses.get(0).getPostalCode();
    String country = addresses.get(0).getCountryName();
    
    0 讨论(0)
  • 2020-11-22 09:39

    You are looking for the term Geocoding.

    The short story is you need to do:

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
    

    To do more you should read up on the Geocoder here.

    0 讨论(0)
提交回复
热议问题