我想从纬度和经度在安卓得到以下值

街道地址 城市/州 邮政编码 完整的地址

如何做到这一点?


当前回答

试试这段代码(正在工作)

public void GetLocation() throws IOException {

    LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
            || (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {

        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
        }, 200);

        return;
    } else {



        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d(TAG, "onLocationChanged: " + location.getLongitude() + " , " + location.getLatitude());

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
                Log.d(TAG, "onStatusChanged: " + s);

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        });
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);

        if (location == null) {
            Toast.makeText(context, "GPS signal not found",
                    Toast.LENGTH_LONG).show();
        }
        if (location != null) {
            Log.e("location", "location--" + location);
            Log.e("latitude at beginning",
                    "@@@@@@@@@@@@@@@" + location.getLatitude());
            // onLocationChanged(location);
        }


        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(context, Locale.getDefault());
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
        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();

        Log.d(TAG, "GetLocation: address " + address + " city " + city + " state " + state + " country " + country + " postalCode " + postalCode + " knownName " + knownName);
    }
}

其他回答

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;
}

您可以使用此方法对正确完整的地址进行地理编码

试试这段代码(正在工作)

public void GetLocation() throws IOException {

    LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
            || (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {

        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
        }, 200);

        return;
    } else {



        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d(TAG, "onLocationChanged: " + location.getLongitude() + " , " + location.getLatitude());

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
                Log.d(TAG, "onStatusChanged: " + s);

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        });
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);

        if (location == null) {
            Toast.makeText(context, "GPS signal not found",
                    Toast.LENGTH_LONG).show();
        }
        if (location != null) {
            Log.e("location", "location--" + location);
            Log.e("latitude at beginning",
                    "@@@@@@@@@@@@@@@" + location.getLatitude());
            // onLocationChanged(location);
        }


        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(context, Locale.getDefault());
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
        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();

        Log.d(TAG, "GetLocation: address " + address + " city " + city + " state " + state + " country " + country + " postalCode " + postalCode + " knownName " + knownName);
    }
}

您正在寻找“地理编码”一词。

你需要做的是:

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

要做更多,你应该在这里阅读Geocoder。

你可以这样做,以获得完整的经纬度地址:

  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));
                    }

您需要传递纬度和经度值。

Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(getContext(), Locale.getDefault());

        try {
            addresses = geocoder. getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
            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

            System.out.println(address+"-------------");
        } catch (IOException e) {
            e.printStackTrace();
        }