How to get complete address from latitude and longitude?

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

提交回复
热议问题