如何使Android中的地理围栏警报更准确

12

你好,我正在尝试在Android中添加Geo Fence功能。我正在使用http://developer.android.com/training/location/geofencing.html创建和监控Geo Fence。我使用IntentService进行提醒(已进入/已退出),但对我来说它没有起作用。

但是当我离开并回到区域以进行测试时,它对我不起作用。我已经打开了设备的GPS,但设备没有连接到互联网。

请问有人可以帮助我使其更加准确、完美,没有任何问题吗?

public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener,
        LocationClient.OnAddGeofencesResultListener {

    private LocationClient locationClient;

    private String TAG = "MainActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resp == ConnectionResult.SUCCESS) {
            locationClient = new LocationClient(this, this, this);
            locationClient.connect();
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        ArrayList<Store> storeList = getStoreList();
        if (null != storeList && storeList.size() > 0) {
            ArrayList<Geofence> geofenceList = new ArrayList<Geofence>();
            for (Store store : storeList) {
                float radius = (float) store.radius;
                Geofence geofence = new Geofence.Builder()
                        .setRequestId(store.id)
                        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                        .setCircularRegion(store.latitude, store.longitude, radius)
                        .setExpirationDuration(Geofence.NEVER_EXPIRE)
                        .build();

                geofenceList.add(geofence);
            }

            PendingIntent geoFencePendingIntent = PendingIntent.getService(this, 0,
                    new Intent(this, GeofenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
            locationClient.addGeofences(geofenceList, geoFencePendingIntent, this);
        }
    }

    @Override
    public void onDisconnected() {
        Log.e(TAG, "Disconnected !");
    }

    @Override
    public void onAddGeofencesResult(int i, String[] strings) {
        if (LocationStatusCodes.SUCCESS == i) {
            //todo check geofence status
        } else {

        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.e(TAG, connectionResult.getErrorCode() + "");
    }

    private ArrayList<Store> getStoreList() {
        ArrayList<Store> storeList = new ArrayList<Store>();
        for (int i = 0; i < 1; i++) {
            Store store = new Store();
            store.id = String.valueOf(i);
            store.address = "India";
            store.latitude = 26.7802187;
            store.longitude = 75.860322;
            store.radius = 100.0;

            storeList.add(store);
        }

        return storeList;
    }

    public class Store {
        String id;
        String address;
        double latitude;
        double longitude;
        double radius;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != locationClient) {
            locationClient.disconnect();
        }
    }
}

GeofenceIntentService.java

public static final String TRANSITION_INTENT_SERVICE = "ReceiveTransitionsIntentService";

public GeofenceIntentService() {
    super(TRANSITION_INTENT_SERVICE);
}

@Override
protected void onHandleIntent(Intent intent) {
    if (LocationClient.hasError(intent)) {
        //todo error process
    } else {
        int transitionType = LocationClient.getGeofenceTransition(intent);
        if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ||
                transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
            List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);

            for (Geofence geofence : triggerList) {
                generateNotification(geofence.getRequestId(), "address you defined");
            }
        }
    }
}

private void generateNotification(String locationId, String address) {
    long when = System.currentTimeMillis();
    Intent notifyIntent = new Intent(this, MainActivity.class);
    notifyIntent.putExtra("id", locationId);
    notifyIntent.putExtra("address", address);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.dac_logo)
                    .setContentTitle(locationId)
                    .setContentText(address)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setWhen(when);    

    NotificationManager notificationManager =   
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify((int) when, builder.build());   
 }
 }
AndroidManifest.xml
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"    
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

<application
    android:allowBackup="true"
    android:icon="@drawable/dac_logo"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:excludeFromRecents="true"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:taskAffinity="" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".GeofenceIntentService"
        android:exported="false" />

    <receiver android:name="com.location.demo.receivers.BootCompleteReceiver" >    
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />    
        </intent-filter>
    </receiver>

    <meta-data
        android:name="com.google.android.gms.version"    
        android:value="@integer/google_play_services_version" />    
</application>


你的应用程序能够接收GPS坐标吗? - Pankaj
2
Android Geofence示例中存在一个错误,因此不建议使用IntentService类,您应该使用BroadcastReceiver来触发进入和退出事件。 - Pankaj
你是怎么导入LocationClient的?SDK里面好像没有这个类。 - IgorGanapolsky
2
@Pankaj “使用IntentService类是不被鼓励的” - 这是无稽之谈。Geofence API甚至不关心您是使用广播接收器还是IntentService。也许自您发表评论以来已经有所改变,但我在我的应用程序中使用IntentService,它运行良好。 - Tim
5个回答

23

我发现地理围栏从未从GPS硬件智能地检索位置。地理围栏API将观察操作系统中可用的最准确位置,或者如果没有最近的位置读数,则会从Wifi/蜂窝数据中计算位置。(因为移动网络定位极其不准确,而Wifi则经常不可用)

所以,要从地理围栏API中得到任何响应或准确的结果,您必须设置好您的地理围栏,然后按照间隔轮询GPS硬件,甚至不使用收到的结果,以便在表面下提供有价值的数据给操作系统。

这可能是为什么您的结果不准确的核心原因。当定位精度为500米(使用蜂窝网络定位时并不罕见)且您的围栏半径为50米时,只有当距离围栏点至少550米时才会产生离开事件,操作系统才能确定您已经100%地离开了该围栏。

简言之,按照一定时间间隔轮询GPS硬件,不对接收到的结果进行任何处理,您将开始获得更准确的地理围栏。


1
是的,我知道您所说的“定期轮询GPS硬件”的方法已经由Google Play服务位置服务完成,但在这里我已经打开了GPS(正如我在问题中已经写过),但仍然无法触发进入/退出事件。您提到的导致结果不准确的情况不适用于我的情况,因为我已经打开了GPS。 - N Sharma
2
你是如何打开GPS的?我相信@stealthwang想说的是,Google Play服务始终会尝试节省电池电量,除非你明确要求它使用GPS。你应该拥有自己的位置监听器,使用LocationManager.GPS_PROVIDER设置LocationManager实例,并请求每秒更新一次位置。请查看LocationManager文档。 - rootkit
4
我建议的并不是让你自己进行地理围栏设置。只需定期轮询GPS,无须对获取的位置数据进行任何操作。这样,Google Play地理围栏就可以使用GPS数据,而不必依赖于低功耗、低精度的定位服务。 - rootkit
1
@Williams 我的回答旨在解释,就我所知,Geofencing API从未单独使用GPS来计算位置。在我自己的应用程序中,模仿API示例代码,尝试修改LocationClient配置后,我无法产生小于约500m半径的围栏入口事件。只要我开始轮询GPS并加入我的地理围栏代码,小至约30m的围栏就能正常工作。 - stealthwang
2
哈哈,它并不是“完美地工作”。它从来没有读取过GPS。小的地理围栏在我开始后台轮询GPS之前从未起作用。你可以相信你想要相信的。 - stealthwang
显示剩余11条评论

2
我从几个StackOverflow线程中总结了有关Android地理围栏的最佳实践,并在Github上创建了一个名为Geofencer的示例项目。这可以用作将GeoFencing实现到您的应用程序的参考。它还允许随时切换到第三方GeoFencing实现。希望这对你们中的一些人有所帮助!

1
Android的原生地理围栏功能精度惊人地低且消耗电量大。我建议您使用第三方工具,例如Bluedot的Point SDK(精度为10米/ 30英尺)或Gimbal的SDK(精度为50米/ 165英尺),两者在可靠性和电池寿命方面都更加可靠。
完全透明:我为Bluedot工作。

5
这些SDK可以方便使用Android/Google API,但它们并不提供额外的更新频率/精度,这些都已经可以使用本地/Google API实现-这些SDK在内部也会利用这些API。事实上,声明准确性数字是不诚实的,因为准确性完全取决于设备的硬件和环境条件。 - TheIT

1
我也遇到了地理围栏系统不断进出我的地理围栏的问题,后来我读到了官方文档中的这段话(引用自官方文档):
“您的地理围栏内没有可靠的网络连接。如果没有可靠的数据连接,则可能无法生成警报。这是因为地理围栏服务依赖于网络位置提供程序,而网络位置提供程序又需要数据连接。” 来自官方地理围栏文档

-5
如果设备未连接到互联网,Google Play服务(如地理围栏或活动识别)将无法工作。

Google Play服务,特别是Google位置服务,不需要互联网即可工作。 - Marian Paździoch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接