How to get complete address from latitude and longitude?

前端 未结 21 2499
深忆病人
深忆病人 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: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
    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 addressFragments = new ArrayList(); // 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

提交回复
热议问题