如何将LatLng发送到proximityintentreceiver

时间:2018-01-23 14:00:59

标签: android android-geofence proximity

此代码按地方名称输入,但我想通过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);

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

更改任务签名以使用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);

        }
    }
}