【问题标题】:How to make Geo Fencing alert more accurate in Android如何在 Android 中使地理围栏警报更准确
【发布时间】:2014-07-05 16:42:28
【问题描述】:

您好,我正在尝试在 Android 中添加地理围栏功能。我正在使用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>

【问题讨论】:

  • 您能否在您的应用中接收 GPS 坐标?
  • android Geofence Sample有一个bug,所以不鼓励使用IntentService类,你应该使用BroadcastReceiver来触发进入和退出事件
  • 你是如何导入 LocationClient 的?它在 SDK 中不可用。
  • @Pankaj “不鼓励使用 IntentService 类” - 废话。无论您使用的是广播接收器还是意图服务,地理围栏 API 都不会。自从您发表评论以来,也许它已经改变了,但是我在我的应用程序中使用了 intentservice,它工作正常

标签: android google-play-services geofencing android-geofence


【解决方案1】:

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

因此,要从地理围栏 API 中获得所有响应性或准确的结果,您必须设置地理围栏,然后每隔一段时间轮询 GPS 硬件,甚至不对收到的结果做任何事情,这样您就可以在表面之下为操作系统提供有价值的数据。

这可能是您的结果不准确的核心原因。直到操作系统确定您 100% 在围栏之外,地理围栏出口才会触发 - 因此,如果位置读数的准确度为 500 米(使用单元地理定位时不太可能)并且您的围栏半径为 50m,那么您d 必须距离您的围栏点至少 550m 才能产生退出事件。

TLDR;每隔一段时间轮询 GPS 硬件而不对结果做任何事情,您将开始获得更准确的地理围栏。

【讨论】:

  • 是的,我知道您告诉“每隔一段时间轮询 GPS 硬件”的方法,该方法已经由 Google Play 服务定位服务完成,但在这里我已经打开了我已经打开的 GPS写在问题中,但仍然没有触发进入/现有事件。你说的为什么结果不准确的情况不适用于我的情况,因为我已经打开了 GPS
  • 以及如何要求在此处查找更准确的位置?
  • 您是如何开启 GPS 的?我相信@stealthwang 试图说 Google Play 服务将始终尝试节省您的电池,并且除非您明确要求,否则不会使用 GPS。您应该有自己的位置监听器,它使用 LocationManager.GPS_PROVIDER 设置 LocationManager 实例,并以 1 秒左右的间隔请求位置更新。检查 LocationManager 文档。
  • 我不是建议你自己做地理围栏。只需定期轮询 GPS,无需对检索到的位置数据进行任何操作。这个想法是,Google Play 地理围栏将使用 GPS 数据,而不是依赖低功耗、低精度的定位服务
  • 哈哈不,它不能“以完美的方式工作”。它从未从 GPS 读取数据。在我开始在后台轮询 gps 之前,小型地理围栏从未起作用。相信你想要的。
【解决方案2】:

我在sample project on Github called Geofencer 中总结了几个 StackOverflow 线程中关于 Android 地理围栏的最佳实践。这可以用作在您的应用程序中实施 GeoFencing 的参考。它还允许即时切换到第 3 方 GeoFencing 实施。我希望这对你们中的一些人有所帮助!

【讨论】:

    【解决方案3】:

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

    您的地理围栏内没有可靠的网络连接。如果 没有可靠的数据连接,可能不会生成警报。 这是因为地理围栏服务取决于网络位置 提供者又需要数据连接。

    From the official geofence documentation

    【讨论】:

      【解决方案4】:

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

      全面披露:我为 Bluedot 工作。

      【讨论】:

      • 这些 SDK 可能有助于使用 Android/Google API,但它们没有提供使用本机/Google API 尚不可用的额外更新频率/准确性 - 这些 SDK 将在内部使用这些 API。事实上,说准确度数字是不诚实的,因为准确度完全取决于设备的硬件和环境条件。
      【解决方案5】:

      如果 che 设备未连接到互联网 google play 服务(如地理围栏或活动识别)将无法工作。

      【讨论】:

      • Google Play 服务,尤其是 Google 位置服务,不需要互联网即可工作。
      猜你喜欢
      • 2015-10-24
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多