使用邻近警报确定POI并获取与当前位置的距离

时间:2015-04-21 14:52:25

标签: android geofencing

我的位置应用再次出现问题。让我解释一下目前的情况:

我的SD卡上存有一个文件,其中包含各种纬度和经度组合,以便定义不同的POI。这些被读入ArrayList并使用标记添加到地图中。此外,还添加了接近警报。每当用户接近POI时,他都会使用BroadcastReceiver通知他。这工作得非常好。

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

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

我的想法是回馈POI的纬度和经度。每当用户输入指定的半径时,就会从BroadcastReceiver向Activity激活一个Intent(这是我添加了邻近警报的MainActivity)。然后,在我的Activity中,我得到纬度和经度,并在onLocationChanged方法中继续我的距离计算。

不幸的是,我的想法并不奏效。它只是让我循环,然后我的手机冻结。以下是一些代码段:

在我的MainActivity中:

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();

    }
}

我感谢任何建议,以解决我的问题。我无法找到解决方案......

0 个答案:

没有答案