Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用接近警报确定POI,并获取其到当前位置的距离_Android_Geofencing - Fatal编程技术网

Android 使用接近警报确定POI,并获取其到当前位置的距离

Android 使用接近警报确定POI,并获取其到当前位置的距离,android,geofencing,Android,Geofencing,又是我的位置应用程序出现了另一个问题。让我解释一下目前的情况: 我的SD卡上存储了一个文件,其中包含纬度和经度的各种组合,以便定义不同的POI。这些将被读取到ArrayList中,并使用标记添加到地图中。此外,还添加了接近警报。每当用户靠近POI时,就会使用广播接收器通知他。这很好用 现在,我要做的是,当用户距离POI 1英里、0.5英里和0.1英里时,向用户显示一条消息 我能做的是在BroadcastReceive()方法的onReceive()中使用distanceTo()方法。但当用户输入

又是我的位置应用程序出现了另一个问题。让我解释一下目前的情况:

我的SD卡上存储了一个文件,其中包含纬度和经度的各种组合,以便定义不同的POI。这些将被读取到ArrayList中,并使用标记添加到地图中。此外,还添加了接近警报。每当用户靠近POI时,就会使用广播接收器通知他。这很好用

现在,我要做的是,当用户距离POI 1英里、0.5英里和0.1英里时,向用户显示一条消息

我能做的是在BroadcastReceive()方法的onReceive()中使用distanceTo()方法。但当用户输入半径时,这只会起作用一次。但由于我需要根据当前位置定期更新距离,这对我没有帮助

我的想法是返回POI的纬度和经度。每当用户输入指定半径时,就会从BroadcastReceiver向活动(这是添加接近警报的my MainActivity)触发意图。然后,在我的活动中,我获得纬度和经度,并继续使用onLocationChanged方法计算距离

不幸的是,我的想法行不通。它只是给我一个循环,然后我的手机就有点僵了。以下是一些代码片段:

在我的主要活动中:

private void addProximityAlert(double latitude, double longitude, int id) {

        // +++ Add a proximity alert to the corresponding obstacle +++ \\

        // Attach the id of the obstacle (its index in the ArrayList) to an intent
        // NOTE: This is needed in order to distinguish between all proximity alerts added
        Intent intent = new Intent(Constants.PROX_ALERT_INTENT);
        intent.putExtra("key_id", id);
        intent.putExtra("key_latitude", latitude);
        intent.putExtra("key_longitude", longitude);
        intent.putExtra("key_current_location", mCurrentLocation);
        PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // Add the proximity alert
        mLocationManager.addProximityAlert(latitude, longitude, Constants.POINT_RADIUS, Constants.PROX_ALERT_EXPIRATION, proximityIntent);
    }

...

public void onLocationChanged(Location location) {
    Toast.makeText(this, counter + "", Toast.LENGTH_LONG).show();

    // +++ Adapt mCurrentLatitude and mCurrentLongitude when position changes +++ \\
    mCurrentLocation = location;
    mCurrentLatitude = mCurrentLocation.getLatitude();
    mCurrentLongitude = mCurrentLocation.getLongitude();

    if(counter == 2) {
        Intent fromReceiver = getIntent();

        if (fromReceiver.getDoubleExtra("key_dest_latitude", 12345) != 12345.0) {

            double latitude = fromReceiver.getDoubleExtra("key_dest_latitude", 12345);
            double longitude = fromReceiver.getDoubleExtra("key_dest_longitude", 12345);

            Toast.makeText(this, "RECEIVER: LAT =" + latitude + " & LONG = " + longitude, Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(this, "NOT AVAILABLE YET", Toast.LENGTH_LONG).show();
        }
    }
}
在BroadcastReceiver类中:

    public void onReceive(Context context, Intent intent) {
    // +++ Check if a geofence is entered +++ \\

    String key = LocationManager.KEY_PROXIMITY_ENTERING;
    mEntering = intent.getBooleanExtra(key, false);

    if (mEntering == true) {
        Toast.makeText(context, "Obstacle " + intent.getIntExtra("key_id", 666) + " in less than " + Constants.POINT_RADIUS + "m!", Toast.LENGTH_LONG).show();

        Intent fromReceiver = new Intent(context.getApplicationContext(), MainActivity.class);
        fromReceiver.putExtra("key_dest_latitude", intent.getDoubleExtra("key_latitude", 0));
        fromReceiver.putExtra("key_dest_longitude", intent.getDoubleExtra("key_longitude", 0));
        fromReceiver.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(fromReceiver);

    }
    else {
        Toast.makeText(context, "Leaving", Toast.LENGTH_LONG).show();

    }
}

为了解决我的问题,我非常感谢任何建议。我只是找不到解决方案…

我想另一种方法是在POI中添加3个接近警报,每个警报都有另一个半径(1英里、0.5英里和0.1英里)。但对我来说,这似乎效率很低,尤其是当存在未知数量的POI时。