android计算中超过100个地理围栏

v6ylcynt  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(296)

我有一个危险区域的列表,里面有大约1000个点和经纬度。现在我要计算每个点到当前位置的距离,这样我只得到100米距离的点。我把它放到另一个名为onlyneargeofines的列表中,然后为onlyneargeofines列表中的所有点添加geofines。为此,我在onmapready中有这样一个代码,在这里进行计算:

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        LatLng berlin = new LatLng(52.409952, 13.355338);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(berlin, 15));

        //mMap.setOnMapLongClickListener(this);

        enableUserLocation();

        if (Build.VERSION.SDK_INT >= 29) {
            // We need background permission (Manifest.xml)
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) { // wenn permission granted

                while (true) {
                    dangerousArea.forEach(coordinate -> {                           // get these points as geofences which are in a distance of 100m from the current position
                        double lat2 = splitIntoLatitude(coordinate);
                        double lon2 = splitIntoLongitude(coordinate);

                        distance=Math.sqrt(((lat2 - currentLat) * (lat2 - currentLat)) + ((lon2 - currentLong) * (lon2 - currentLong)))*100000;// Meter
                        if (distance<100){
                            if (onlyNearGeofences.contains(coordinate)) {}
                            else {
                                onlyNearGeofences.add(new LatLng(lat2, lon2));
                            }

                        }
                        else{
                            if (onlyNearGeofences.contains(coordinate))
                                onlyNearGeofences.remove(coordinate);
                            else {}
                        };});
                    onlyNearGeofences.forEach(coord -> {tryAddingGeofence(coord);});

                    try {
                        Thread.sleep(15 * 1000); //milliseconds
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            } else {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)){
                    //We show a dialog and ask for permission
                    ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, BACKGROUND_LOCATION_ACCESS_REQUEST_CODE);
                } else {
                    ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, BACKGROUND_LOCATION_ACCESS_REQUEST_CODE);
                }

            }
        } else {
            while (true) {
                dangerousArea.forEach(coordinate -> {                           // von der letzten Position sollen die Punkte als Geofences eingebaut werden, die im Umkreis von 50 Metern sind.
                    double lat2 = splitIntoLatitude(coordinate);
                    double lon2 = splitIntoLongitude(coordinate);

                    distance=Math.sqrt(((lat2 - currentLat) * (lat2 - currentLat)) + ((lon2 - currentLong) * (lon2 - currentLong)))*100000;// Meter
                    if (distance<100){
                        if (onlyNearGeofences.contains(coordinate)) {}
                        else {
                            onlyNearGeofences.add(new LatLng(lat2, lon2));
                        }

                    }
                    else{
                        if (onlyNearGeofences.contains(coordinate))
                            onlyNearGeofences.remove(coordinate);
                        else {}
                    };});
                onlyNearGeofences.forEach(coord -> {tryAddingGeofence(coord);});

                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

我试着将一些纬度和经度设置为“currentlat”和“currentlong”,而不是使用实际位置。所以我发现问题出在计算上。当我在这个代码位置执行其他操作时,它将被执行(例如,我手动向列表中添加点,并在这些点上放置地理围栏,它就工作了。)但是如果我将计算放在这个代码位置,它就不再工作了。所以一定是出了问题。这是额外的计算:

while (true) {
                dangerousArea.forEach(coordinate -> {                           // get those points as geofences which are in a distance of 100m from the current position
                    double lat2 = splitIntoLatitude(coordinate);
                    double lon2 = splitIntoLongitude(coordinate);

                    distance=Math.sqrt(((lat2 - currentLat) * (lat2 - currentLat)) + ((lon2 - currentLong) * (lon2 - currentLong)))*100000;// Meter
                    if (distance<100){
                        if (onlyNearGeofences.contains(coordinate)) {}
                        else {
                            onlyNearGeofences.add(new LatLng(lat2, lon2));
                        }

                    }
                    else{
                        if (onlyNearGeofences.contains(coordinate))
                            onlyNearGeofences.remove(coordinate);
                        else {}
                    };});
                onlyNearGeofences.forEach(coord -> {tryAddingGeofence(coord);});

                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

这个公式有效。我在互联网上找到了它,并在eclipse中尝试了它,我发现它得到了以米为单位的正确距离。那么为什么android不做计算呢?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题