如何让Geo Fencing在Android中更加准确



你好,我正在尝试在Android中添加Geo Fence的功能。我正在使用http://developer.android.com/training/location/geofencing.html创建和监控地理围栏。我正在使用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>

我发现GeoFencing从来没有智能地从GPS硬件检索位置。GeoFence API将从操作系统中观察到最准确的位置,或者如果没有可用的最近位置读数,它将导致从Wifi/蜂窝计算位置。(这很糟糕,因为蜂窝网络非常不准确,wifi经常不可用)

因此,要从Geofencing API获得所有响应或准确的结果,您必须设置您的Geofencing,然后轮询GPS硬件的间隔,甚至不做任何与收到的结果,所以在表面下,您提供有价值的数据给操作系统。

这可能是你的结果不准确的核心原因。地理围栏退出不会触发,直到操作系统确定你100%在围栏之外-所以如果位置读数的精度为500米(当使用小区地理定位时不是不可能的),而你的围栏半径为50米,你必须至少距离围栏点550米才能产生退出事件。

TLDR;在一段时间内轮询GPS硬件,而不做任何与结果,你会开始得到更准确的地理围栏。

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

我也遇到了地理围栏系统跳进跳出我的地理围栏的问题,然后我读到这个(引用自官方文档):

您的地理围栏内没有可靠的网络连接。如果没有可靠的数据连接,可能不会生成警报。这是因为地理防护服务依赖于网络位置提供程序,它反过来需要一个数据连接。

来自官方地理围栏文档

Android的原生地理围栏非常不准确,而且非常耗电。我建议你使用第三方工具,如Bluedot的Point SDK(精确到10米/30英尺)或Gimbal的SDK(精确到50米/165英尺),这两种工具都更可靠,电池消耗更少。

完全披露:我为Bluedot工作

如果设备没有连接到互联网google play服务(如地理围栏或活动识别)不工作

最新更新