首页 > 解决方案 > 当设备在标记的 10 米内时调用该方法

问题描述

在我的应用程序中,我获得了设备的坐标。现在我正在尝试使设备在 10 米半径内时调用该方法。找到 addProximityAlert 方法。但是你需要传递我需要的点的坐标,但我不知道如何链接我需要的标记和设备的位置。我已经创建了一个广播接收器。请提供建议或文章。

public void getlocate() {
        if (flag) {
            Intent intent = new Intent("com.runline.workertrack");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, -1, intent, 0);
            try {
                enable = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (enable) {
                if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5, listener);
                manager.addProximityAlert(0,0 , 100, -1, pendingIntent);
            }
    }
}
public boolean checkLocationPermission()
{
    int loc1 = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listper = new ArrayList<>();
if(loc1 != PackageManager.PERMISSION_GRANTED)
    listper.add(Manifest.permission.ACCESS_FINE_LOCATION);
if(!listper.isEmpty()) {
ActivityCompat.requestPermissions(this, listper.toArray(new String[listper.size()]), 1);

}

    return true;
}

    class Locate implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                manager.removeUpdates(listener);
                shirota = "" + location.getLatitude();
                dolgota = "" + location.getLongitude();
                shir.setText(shirota);
                dolg.setText(dolgota);
                final Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("shirota", shirota);
                hashMap.put("dolgota", dolgota);
                hashMap.put("id", "1");
                reference.setValue(hashMap);
                IntentFilter filter = new IntentFilter("com.runline.workertrack");
                registerReceiver(new ProximityIntentReceiver(), filter);
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    }
public class ProximityIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive (Context context, Intent intent) {
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Boolean entering = intent.getBooleanExtra(key, false);
        if(entering){

            Toast.makeText(getApplicationContext(), "You are here!", Toast.LENGTH_LONG).show();
        }

    }
}

标签: javaandroidgeolocationgps

解决方案


推荐阅读