【问题标题】:how to send LatLng to proximityintentreceiver如何将 LatLng 发送到proximityintentreceiver
【发布时间】:2018-01-23 14:00:58
【问题描述】:

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

            }
        }
    }

【问题讨论】:

    标签: android android-geofence proximity


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-24
      • 2014-02-15
      • 2021-01-20
      • 1970-01-01
      • 2010-10-26
      • 2011-04-09
      • 2014-03-13
      • 1970-01-01
      相关资源
      最近更新 更多