Define POI with proximity alerts and get distance to current position

I got another problem with my location app again. Let me explain the current situation:

I have a file stored on my SD card that contains various combinations of latitudes and longitudes to define different POIs. They are read into an ArrayList and added to the map using markers. In addition, proximity alerts are added. Whenever the user approaches the POI, he is notified by the BroadcastReceiver. This works great.

Now I want to show a message to the user when he is 1 mile, miles and 0.1 miles from the POI.

What I can do is use the distanceTo () method in the onReceive () method of the BroadcastReceiver. But this will only work once, when the user enters the radius. But since I need the distance to be updated regularly based on the current position, that doesn't help me.

My idea is to return the latitude and longitude of the POI. Whenever the user enters the specified radius, the Intent is fired from the BroadcastReceiver in the Activity (this is my MainActivity that added proximity alerts). Then, in my activity, I get the latitude and longitude and continue calculating the distance in the onLocationChanged method.

Unfortunately my idea doesn't work. It just gives me a loop and then my cell phone freezes up. Here are some code snippets:

In my 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();
        }
    }
}

      

In the BroadcastReceiver class:

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

    }
}

      

I am grateful for any suggestions to resolve my issue. I just can't seem to find a solution for this ...

+3


source to share





All Articles