如何将 LatLng 发送到邻近意图接收器



这段代码是按地名输入的,但我想用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);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新