【问题标题】:how to get accurate geofencing event如何获得准确的地理围栏事件
【发布时间】:2017-07-18 17:40:46
【问题描述】:

试图监控设备进入或退出地理围栏。但有时即使设备没有移动,它也会收到进入或退出事件。代码sn-p如下。

使用后也注意到了

lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

创建地理围栏,然后在

void onLocationChanged(Location location)

并使用更新后的位置来获取用于地理围栏的位置之间的距离,似乎也不准确。就像在从位置 #1 创建地理围栏后步行仅几步之遥时,onLocationChanged() 返回位置 #2,并且 distanceTo() 返回显示超过 200 米。

还注意到即使设备在桌子上没有移动,onLocationChanged(Location location) 有时也会返回不同的位置。

我想这可能取决于提供商,但测试在 wifi 覆盖区域内。也许它就是这样工作的。

但是如果需要获得更准确或更精细的信息,是否有不同或更好的方法来获得更准确的地理围栏事件或准确的当前位置?

 googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
createGeofence(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()));

地理围栏创建后,即使设备放在桌子上不动,它也会定期接收Geofence.GEOFENCE_TRANSITION_ENTER和Geofence.GEOFENCE_TRANSITION_EXIT。有时它会在没有事件的情况下保持静止,但如果稍微移动设备一点,它就会收到这些事件。

private void createGeofence(LatLng latlng) {
    GeofencingClient mGeofencingClient; mGeofencingClient = LocationServices.getGeofencingClient(this);

        Geofence geofence = makeOneGeofence(GEOFENCE_REQUEST_ID, latlng, 150f); 
        GeofencingRequest geofenceRequest = createGeofenceRequest(geofence);

        LocationServices.GeofencingApi.addGeofences(
                googleApiClient,
                geofenceRequest,
                getGeofencePendingIntent()
        ).setResultCallback(this);
}

Geofence makeOneGeofence(String reqestId, LatLng latLng, float radius) {
    return new Geofence.Builder()
            .setRequestId(reqestId)
            .setCircularRegion(
                    latLng.latitude,
                    latLng.longitude,
                    radius  //radius, meters   
            ).setExpirationDuration(60 * 60 * 1000)  //1 hr
             .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                    Geofence.GEOFENCE_TRANSITION_DWELL |
                    Geofence.GEOFENCE_TRANSITION_EXIT)
            .setLoiteringDelay(500000)  // 500 secs, after user enters a geofence if the user stays inside during this period of time
            .build();
}

private GeofencingRequest createGeofenceRequest(Geofence geofence) {
    return new GeofencingRequest.Builder()
            .setInitialTrigger( GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL )
            .addGeofence( geofence )
            .build();
}

private final int GEOFENCE_REQUEST_CODE = 0;
private PendingIntent getGeofencePendingIntent() {

    if ( mGeofencePendingIntent != null )
        return mGeofencePendingIntent;

    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(
            this, GEOFENCE_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

【问题讨论】:

    标签: android location android-geofence


    【解决方案1】:

    GPS 信号因设备而异,并且如果您的设备的 GPS 信号不好(如果您在建筑物内或类似的地方),您会看到 GPS 位置在跳动并且不是恒定的。 目前没有很好的方法在 100% 的情况下获得准确的位置。

    【讨论】:

    • 感谢萨吉米蒙!但可能有某种方式,因为某些应用程序似乎与 LocationServices 一起工作非常可靠。只是想知道是否有人知道。
    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 2018-03-20
    • 2014-07-05
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多