How do I use the Android Geofencing API?

蓝咒 提交于 2019-11-26 19:44:55

问题


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?


回答1:


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, i.e. 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.




回答2:


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.




回答3:


in the case network condition you must Increase the radius at least 1000 M ,because using network provider to retrieving the current location,it take location from near by network tower.but in the case of GPS and wifi condition it works more accurate(1 M also).




回答4:


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!

:)




回答5:


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.



来源:https://stackoverflow.com/questions/16769653/how-do-i-use-the-android-geofencing-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!