Android 如何获得准确的地理击剑比赛
正在尝试监视设备进入或退出地理围栏。但有时,即使设备不移动,它也会收到进入或退出事件。下面是代码片段 使用后也会注意到Android 如何获得准确的地理击剑比赛,android,location,android-geofence,Android,Location,Android Geofence,正在尝试监视设备进入或退出地理围栏。但有时,即使设备不移动,它也会收到进入或退出事件。下面是代码片段 使用后也会注意到 lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 要创建地理围栏,请在 void onLocationChanged(Location location) 而使用更新后的位置来获取地理围栏所用位置之间的距离,似乎也不准确。就像从位置#1创建地理围栏后只走了几步
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
要创建地理围栏,请在
void onLocationChanged(Location location)
而使用更新后的位置来获取地理围栏所用位置之间的距离,似乎也不准确。就像从位置#1创建地理围栏后只走了几步,onLocationChanged()返回位置#2,distanceTo()返回显示超过200米
还要注意的是,即使设备在桌子上没有移动,onLocationChanged(位置更改)有时也会返回不同的位置
我想这可能取决于提供商,但测试是在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.geofence\u TRANSITION\u ENTER和geofence.geofence\u TRANSITION\u 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);
}
GPS信号因设备而异,如果您的设备的GPS信号不好(如果您在建筑物内或类似的地方),您会看到GPS位置在跳跃,并且不是恒定的。
目前,在100%的情况下,没有很好的方法获得准确的位置。谢谢Sagi Mymon!但可能有一些方法,因为有些应用程序似乎和LocationServices配合使用非常可靠。只是想知道是否有人知道。