Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am currently working with the Cordova Background Geolocation plugin and have encountered an issue where clearPolygon and loadPolygon behave inconsistently. Specifically, I have noticed that: #1469

Open
yunhoa opened this issue Feb 6, 2025 · 11 comments

Comments

@yunhoa
Copy link

yunhoa commented Feb 6, 2025

I am currently working with the Cordova Background Geolocation plugin and have encountered an issue where clearPolygon and loadPolygon behave inconsistently. Specifically, I have noticed that:

At times, the clearPolygon log appears, followed by loadPolygon, indicating that the geofence is being reloaded properly.
However, in some instances, only clearPolygon appears, and loadPolygon does not follow, meaning the geofence is not being reloaded as expected.
I would like to understand the underlying logic behind this behavior. In what conditions does loadPolygon fail to execute after clearPolygon? Are there specific scenarios, such as geofence persistence settings, plugin lifecycle states, or API calls, that might prevent loadPolygon from running?

I would greatly appreciate any insights or documentation references regarding this behavior.

@christocracy
Copy link
Member

You can get more information in the API docs at Geofence.vertices

  • loadPolygon is called when the device enters the "containing circular geofence" (the green circle).
  • clearPolygon is called when the device exits the "containing circular geofence".
Image

@yunhoa
Copy link
Author

yunhoa commented Feb 7, 2025

package com.htpchem.mysafety ----------------------------
2025-02-05 21:56:43.054 15050-15337 nativeloader com.htpchem.mysafety D Load /data/app/~~K2F8yT9VpP07yI9wZrZI6A==/com.htpchem.mysafety-XaggLYSsp_DoacAE-7JovQ==/base.apk!/lib/arm64-v8a/libtslocationmanager.so using ns clns-4 from class loader (caller=/data/app/~~K2F8yT9VpP07yI9wZrZI6A==/com.htpchem.mysafety-XaggLYSsp_DoacAE-7JovQ==/base.apk!classes20.dex): ok
2025-02-05 21:56:43.148 15050-15337 TSLocationManager com.htpchem.mysafety D $ ready()
2025-02-05 21:56:43.154 15050-15337 TSLocationManager com.htpchem.mysafety D $ getCurrentPosition()
2025-02-05 21:56:43.157 15050-15337 TSLocationManager com.htpchem.mysafety D $ addGeofence()
2025-02-05 21:56:43.158 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] Circle coordinate: 37.565936, 126.838959, radius: 142.270606
2025-02-05 21:56:43.158 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] squared_radius: 0.000002, corrected radius: 142.270606
2025-02-05 21:56:43.158 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] isValid?: 1, error: 0.000000
2025-02-05 21:56:43.175 15050-15337 TSLocationManager com.htpchem.mysafety D $ getGeofences()
2025-02-05 21:56:43.756 15050-15337 TSLocationManager com.htpchem.mysafety D $ addGeofence()
2025-02-05 21:56:43.766 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] Circle coordinate: 37.001909, 126.344182, radius: 2041.999657
2025-02-05 21:56:43.766 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] squared_radius: 0.000337, corrected radius: 2041.999657
2025-02-05 21:56:43.766 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] isValid?: 1, error: 0.000000
2025-02-05 21:56:43.769 15050-15337 TSLocationManager com.htpchem.mysafety D $ getCurrentPosition()
2025-02-05 21:56:43.770 15050-15337 TSLocationManager com.htpchem.mysafety D $ addGeofenceListener()
2025-02-05 21:56:43.771 15050-15337 TSLocationManager com.htpchem.mysafety D $ addLocationListener()
2025-02-05 21:56:43.772 15050-15337 TSLocationManager com.htpchem.mysafety D $ addHeartbeatListener()
2025-02-05 21:56:43.774 15050-15337 TSLocationManager com.htpchem.mysafety D $ start()
2025-02-05 21:56:43.776 15050-15453 TSLocationManager com.htpchem.mysafety I - Enable: false → true, trackingMode: 1
2025-02-05 21:56:43.810 15050-15453 TSLocationManager com.htpchem.mysafety V [JNI geofencing] loadPolygon big
2025-02-05 21:56:44.035 15050-15450 TSLocationManager com.htpchem.mysafety V [JNI geofencing] loadPolygon big
2025-02-05 21:56:48.614 15050-15337 TSLocationManager com.htpchem.mysafety D $ getCurrentPosition()

@yunhoa
Copy link
Author

yunhoa commented Feb 7, 2025

I appreciate your previous response regarding the behavior of clearPolygon and loadPolygon. However, I have observed inconsistencies when adding geofences, and I would like to clarify the underlying logic.

From my logs, I can see that even when my addGeofence method executes successfully, the V [JNI geofencing] loadPolygon big event does not always trigger. I understand that loadPolygon is supposed to be called when the device enters the "containing circular geofence," but in some cases, it does not trigger at all.

I would like to understand the exact mechanics behind loadPolygon, specifically:

When using addGeofence via a custom method
Does calling addGeofence guarantee that loadPolygon will be triggered, or does it depend on the current device location and proximity to the geofence?
When obtaining the current location before creating a geofence
If I retrieve the current position first and then add a geofence, does this affect whether loadPolygon is triggered?
Is there a need to force re-evaluating geofences after adding them?
When creating a geofence before obtaining the current location
If I add a geofence first and then request the current location, could this prevent loadPolygon from triggering?
Would it be necessary to manually force a geofence check in this case?
Given these scenarios, are there any recommended best practices to ensure that loadPolygon reliably triggers when expected? Is there a way to manually invoke a process that forces the geofencing system to check and activate the polygon?

@christocracy
Copy link
Member

christocracy commented Feb 7, 2025

The plug-in begins monitoring a polygon when the native, containing circular geofence fires. This containing circular geofence is monitored by the OS. You don’t need to do anything, such as calling .getCurrentPosition.

When the OS says you’re inside this circular geofence, the plug-in begins monitoring that polygon. When the OS says you’ve exited, the plug-in stops monitong that polygon.

Simple.

@yunhoa
Copy link
Author

yunhoa commented Feb 9, 2025

However, I’m noticing an issue in the logs:

[V JNI geofencing] loadPolygon big

The logs confirm that the plugin has loaded the polygon, yet the ENTER event for that polygon sometimes does not fire (or fires only occasionally). I’d like to understand the exact timing or conditions that trigger the ENTER event for the polygon.

From my understanding:

  1. The operating system monitors the “containing circular geofence.”

  2. When the OS says I have entered that circular geofence, the plugin begins actively monitoring the polygon (that’s when I see “loadPolygon big” in the logs).

  3. At some point, the plugin’s internal logic should detect if I am actually within the polygon boundaries, and then trigger the onGeofence(ENTER) event.

In practice, I see “loadPolygon big” in the logs, but sometimes I do not see the onGeofence(ENTER) event afterward, even though my position is well inside that polygon. Could you clarify the following?

When exactly does the plugin decide to dispatch the ENTER event after it loads the polygon?

Is there a specific additional location update or an explicit OS callback required for the ENTER event to fire?

Do I need to call anything like getCurrentPosition() manually, or should the plugin do that automatically?

Is there any minimum movement or time threshold after "loadPolygon" before the plugin will confirm an ENTER?

Any insights or best practices about polygon geofences that might explain why ENTER sometimes does not occur would be greatly appreciated.

@christocracy
Copy link
Member

christocracy commented Feb 9, 2025

Do I need to call anything like getCurrentPosition() manually

The plug-in automatically requests location once per second after you see loadPolygon and asks “am I in the polygon?” with each location.

I suggest you draw your polygon on a map and show it to me.

@yunhoa
Copy link
Author

yunhoa commented Feb 10, 2025

Image

Our polygon is set up as shown, and loadPolygon has occurred, but the ENTER event has not been triggered. The users represented by the red dots are experiencing the bug where the ENTER event does not fire even though they are inside the polygon.

@christocracy
Copy link
Member

I suggest you provide an entire log file from the plug-in. See api docs .emailLog to learn how to obtain the logs.

@christocracy
Copy link
Member

I don’t suggest using such a large polygon. It will cause high battery drain while users are within and you’re encompassing an entire neighbourhood. If some user both works and lives in that neighborhood, the location api will often be turned on for long periods of time.

You should just use a large circular geofence (or series of smaller ones.)

@yunhoa
Copy link
Author

yunhoa commented Feb 10, 2025

package com.htpchem.mysafety ----------------------------
2025-02-05 21:56:43.054 15050-15337 nativeloader com.htpchem.mysafety D Load /data/app/~~K2F8yT9VpP07yI9wZrZI6A==/com.htpchem.mysafety-XaggLYSsp_DoacAE-7JovQ==/base.apk!/lib/arm64-v8a/libtslocationmanager.so using ns clns-4 from class loader (caller=/data/app/~~K2F8yT9VpP07yI9wZrZI6A==/com.htpchem.mysafety-XaggLYSsp_DoacAE-7JovQ==/base.apk!classes20.dex): ok
2025-02-05 21:56:43.148 15050-15337 TSLocationManager com.htpchem.mysafety D $ ready()
2025-02-05 21:56:43.154 15050-15337 TSLocationManager com.htpchem.mysafety D $ getCurrentPosition()
2025-02-05 21:56:43.157 15050-15337 TSLocationManager com.htpchem.mysafety D $ addGeofence()
2025-02-05 21:56:43.158 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] Circle coordinate: 37.565936, 126.838959, radius: 142.270606
2025-02-05 21:56:43.158 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] squared_radius: 0.000002, corrected radius: 142.270606
2025-02-05 21:56:43.158 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] isValid?: 1, error: 0.000000
2025-02-05 21:56:43.175 15050-15337 TSLocationManager com.htpchem.mysafety D $ getGeofences()
2025-02-05 21:56:43.756 15050-15337 TSLocationManager com.htpchem.mysafety D $ addGeofence()
2025-02-05 21:56:43.766 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] Circle coordinate: 37.001909, 126.344182, radius: 2041.999657
2025-02-05 21:56:43.766 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] squared_radius: 0.000337, corrected radius: 2041.999657
2025-02-05 21:56:43.766 15050-15337 TSLocationManager com.htpchem.mysafety V [MiniBall] isValid?: 1, error: 0.000000
2025-02-05 21:56:43.769 15050-15337 TSLocationManager com.htpchem.mysafety D $ getCurrentPosition()
2025-02-05 21:56:43.770 15050-15337 TSLocationManager com.htpchem.mysafety D $ addGeofenceListener()
2025-02-05 21:56:43.771 15050-15337 TSLocationManager com.htpchem.mysafety D $ addLocationListener()
2025-02-05 21:56:43.772 15050-15337 TSLocationManager com.htpchem.mysafety D $ addHeartbeatListener()
2025-02-05 21:56:43.774 15050-15337 TSLocationManager com.htpchem.mysafety D $ start()
2025-02-05 21:56:43.776 15050-15453 TSLocationManager com.htpchem.mysafety I - Enable: false → true, trackingMode: 1
2025-02-05 21:56:43.810 15050-15453 TSLocationManager com.htpchem.mysafety V [JNI geofencing] loadPolygon big
2025-02-05 21:56:44.035 15050-15450 TSLocationManager com.htpchem.mysafety V [JNI geofencing] loadPolygon big
2025-02-05 21:56:48.614 15050-15337 TSLocationManager com.htpchem.mysafety D $ getCurrentPosition()

@christocracy
Copy link
Member

I’m not interested in tiny snippets of logs. I want the entire log-file (3 days worth)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants