Android 如何将LatLng发送至近端接收器

Android 如何将LatLng发送至近端接收器,android,android-geofence,proximity,Android,Android Geofence,Proximity,这个代码是按地名输入的,但我想按latlng输入。我该怎么办 private class GeocoderTask extends AsyncTask<String, Void, List<Address>> { @Override protected List<Address> doInBackground(String... locationName) { // Creating an instan

这个代码是按地名输入的,但我想按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而不是字符串。然后使用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);

        }
    }
}