简体   繁体   中英

How do I use the Android Geofencing API?

I have been testing the new Google Play Geofencing service API.

I downloaded the sample code from the Android developers site . Next, I ran the example code on an Android device (Galaxy Note 2). I placed my office geo-position and radius to 10m, but when I walked to my office, nothing happened.

While running the sample code, one thing I have noticed is that when I am already inside the geofence range and then add the geofence to LocationClient , nothing happens.

I had already read LocationClient class documentation , and found the following paragraph:

In case network location provider is disabled by the user, the geofence service will stop updating, all registered geofences will be removed and an intent is generated by the provided pending intent. In this case, hasError(Intent) returns true and getErrorCode(Intent) returns GEOFENCE_NOT_AVAILABLE ."

So I turned on WiFi, and was walking to my office (inside the geofence), and then I got notification "geofence entered".

I have the following questions:

  1. Does geofencing only work with WiFi enabled?

  2. Why doesn't the location provided by my 3G network work?

  3. Is this a bug in the sample code?

  4. Or is this my mistake?

If you only use cellular network, the location accuracy is lower, than the accuracy of wi-fi or gps (if you open Google Maps, the blue circle is estimation of your current position - if you only use 3GP, the circle can have radius of even hundreds of meters).

Last days I am working on an app using Geofences and I figured out the following: IMHO the geofence entry is triggered only if ALL this "location estimation circle" is inside the geofence radius, ie if there is 100 % chance that you entered the geofence. So it depends on how big radius you set. If you set only 10 metres, it will probably work only with GPS, never with 3GP. For 3GP you will have to set radius like 1000 metres.

Wifi is used to help track your location. Using wifi signals and cell signals to triangulate your position is a more power efficient solution than having your GPS running constantly. Additionally it works in situations where GPS wouldn't such as heavy multipath areas and indoors.

在网络情况下你必须增加半径至少1000 M,因为使用网络提供商来检索当前位置,它从网络塔附近的位置。但在GPS和wifi条件的情况下它工作更准确(1 M也)。

I thought I'd share my results here. I too have developed a lab app that implements Google Play Services Location API and Geofence API. My results show that the location determination code has a sort of pathological inability to admit its mistakes. :)

When a button is pressed my app creates a geofence at the current location ("curLoc") returned by the location API with a 30 meter radius. On the screen I'm displaying the distance to the geofence center ("distToCenter") by calling curLoc.distanceTo(fenceLoc) and updating it roughly every second.

I start the app and wait until the accuracy stabilizes (normally at 10.0 meters), then press the button to create the fence. I get the "enter" event immediately. I clear the event and go for a walk and apparently enter a space-time anomaly. I walk down the block and around the corner, and the distToCenter value has only increased by about 4 meters. I go further until I'm about 300 meters from my house (judging by counting steps) and the dstToCenter gradually increases. The geofence exit event finally fires about 20 seconds AFTER the dstToCenter goes above 30 meters.

I wait to see where the distance stabilizes and it's about 150-ish meters. I turn around and head back home and the same extreme position lag occurs. I sit down at my desk while my wormhole self is still about 90 meters away. After about 40 seconds my doppleganger gets within the fence and after about 20 more seconds I get the enter event.

At this point I'm still somewhere on the front sidewalk (7.4 meters away), inching my way to the front door.

NOTE: The source was "fused" throughout although I did see the GPS status bar icon come on and flash for a few seconds after I'd lost connectivity to my WiFi. What's more, the accuracy never wavered from 10.0 meters.

LIARS!

:)

I tested sample geofence application, and if WiFi is Off it does not detect geofence events on version less than Android 4.3.

Android 4.3 and above works even WiFi is Off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM