How to get complete address from latitude and longitude?

前端 未结 21 2078
深忆病人
深忆病人 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:47

    You can do like this to get complete address from latitude and longitude :

      public class MainActivity extends AppCompatActivity {
    
             ...
    
      private Geocoder geocoder;
      private TextView mAddressTxtVu;
    
             ...
    
    
      // I assume that you got latitude and longitude correctly 
    
      mLatitude  =  20.23232
      mLongitude =  32.999
    
      String errorMessage = "";
    
      geocoder = new Geocoder(context, Locale.getDefault());
    
      List<Address> addresses = null;
    
      try {
                  addresses = geocoder.getFromLocation(
                           mlattitude,
                           mlongitude,
                           1);
          } catch (IOException e) {
                  errorMessage = getString(R.string.service_not_available);
                  Log.e(TAG, errorMessage, e);
          } catch (IllegalArgumentException illegalArgumentException) {
                            // Catch invalid latitude or longitude values.
                  errorMessage = getString(R.string.invalid_lat_long_used);
                  Log.e(TAG, errorMessage + ". " + "Latitude = " + mlattitude +", Longitude = " + mlongitude, illegalArgumentException);
          }
    
          // Handle case where no address was found.
          if (addresses == null || addresses.size() == 0) {
                 if (errorMessage.isEmpty()) {
                          errorMessage = getString(R.string.no_address_found);
                          Log.e(TAG, errorMessage);
                 }
    
          } else {
                 Address address = addresses.get(0);
                 ArrayList<String> addressFragments = new ArrayList<String>();
    
                 // Fetch the address lines using getAddressLine,
                 // join them, and send them to the thread.
                 for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
                          addressFragments.add(address.getAddressLine(i));
                 }
                 // Log.i(TAG, getString(R.string.address_found));
    
    
        mAddressTxtVu.setText(TextUtils.join(System.getProperty("line.separator"),
                                    addressFragments));
                        }
    
    0 讨论(0)
  • 2020-11-22 09:49

    There is a last trick to get Address from Lat-Long (Geo-coordinates). You can simply hit google-maps web service passing the Latitude and longitude. It is simply a GET-Method web-service.

    It will return the JSON Response that can be parsed easily to get address. The URL for this is:

    http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true
    

    You can replace 32,75 with lat,long.

    0 讨论(0)
  • 2020-11-22 09:49

    Just Use this method and pass your lat, long.

    public static void getAddress(Context context, double LATITUDE, double LONGITUDE{
        //Set Address
        try {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
    
            if (addresses != null && addresses.size() > 0) {
                String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                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(); // Only if available else return NULL
                Log.d(TAG, "getAddress:  address" + address);
                Log.d(TAG, "getAddress:  city" + city);
                Log.d(TAG, "getAddress:  state" + state);
                Log.d(TAG, "getAddress:  postalCode" + postalCode);
                Log.d(TAG, "getAddress:  knownName" + knownName);
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }
    
    0 讨论(0)
提交回复
热议问题