如何将 LatLng 发送到 proximityintentreceiver

how to send LatLng to proximityintentreceiver

此代码是按地名输入的,但我想按latlng 输入。我该怎么办?

private class GeocoderTask extends AsyncTask<String, Void, List<Address>> {

        @Override
        protected List<Address> doInBackground(String... locationName) {
            // Creating an instance of Geocoder class
            Geocoder geocoder = new Geocoder(getContext());
            List<Address> addresses = null;
            try {
                addresses = geocoder.getFromLocationName(locationName[0], 5);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return addresses;
        }

        @SuppressLint("MissingPermission")
        @Override
        protected void onPostExecute(List<Address> addresses) {

            //Log.i("Location","address = "+addresses);

            if (addresses == null || addresses.size() == 0) {
                Toast.makeText(getContext(), "No Location found", Toast.LENGTH_SHORT).show();
            }

            if (addresses != null) {
                Toast.makeText(getContext(), "Location found", Toast.LENGTH_SHORT).show();
                for (int i = 0; i < addresses.size(); i++) {

                    Address address = addresses.get(i);
                    latLng = new LatLng(address.getLatitude(), address.getLongitude());

                    String addressText = String.format("%s, %s",
                            address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                            address.getCountryName());

                    markerOptions = new MarkerOptions();
                    markerOptions.position(latLng);
                    markerOptions.title(addressText);

                    CircleOptions circleOptions = new CircleOptions();
                    circleOptions.center(latLng);
                    circleOptions.radius(Rd);


                    Intent intent = new Intent("com.example.a57050358.testboat.util.proximityintentreceiver");
                    PendingIntent proximityIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);
//intent to proximity
                    locationManager.addProximityAlert(address.getLatitude(), address.getLongitude(), Rd, -1, proximityIntent);

                }
                IntentFilter filter = new IntentFilter ("com.example.a57050358.testboat.util.proximityintentreceiver");
                getContext().registerReceiver(new ProximityIntentReceiver(), filter);

            }
        }
    }

更改任务签名以使用 LatLng 而不是 String。然后使用 geocoder.getFromLocation 而不是 geocoder.getFromLocationName

private class GeocoderTask extends AsyncTask<LatLng, Void, List<Address>> {

    @Override
    protected List<Address> doInBackground(LatLng... locations) {
        // Creating an instance of Geocoder class
        Geocoder geocoder = new Geocoder(getContext());
        List<Address> addresses = null;
        try {
            addresses = geocoder.getFromLocation(locations[0].latitude, locations[0].longitude, 5);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }

    @SuppressLint("MissingPermission")
    @Override
    protected void onPostExecute(List<Address> addresses) {

        //Log.i("Location","address = "+addresses);

        if (addresses == null || addresses.size() == 0) {
            Toast.makeText(getContext(), "No Location found", Toast.LENGTH_SHORT).show();
        }

        if (addresses != null) {
            Toast.makeText(getContext(), "Location found", Toast.LENGTH_SHORT).show();
            for (int i = 0; i < addresses.size(); i++) {

                Address address = addresses.get(i);
                latLng = new LatLng(address.getLatitude(), address.getLongitude());

                String addressText = String.format("%s, %s",
                        address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                        address.getCountryName());

                markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                markerOptions.title(addressText);

                CircleOptions circleOptions = new CircleOptions();
                circleOptions.center(latLng);
                circleOptions.radius(Rd);


                Intent intent = new Intent("com.example.a57050358.testboat.util.proximityintentreceiver");
                PendingIntent proximityIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);

                locationManager.addProximityAlert(address.getLatitude(), address.getLongitude(), Rd, -1, proximityIntent);

            }
            IntentFilter filter = new IntentFilter ("com.example.a57050358.testboat.util.proximityintentreceiver");
            getContext().registerReceiver(new ProximityIntentReceiver(), filter);

        }
    }
}