2017-07-18 1 views
0

デバイスを監視しようとすると、ジオフェンスを入力または終了します。デバイスが動いていなくても入力または終了イベントを受信することがあります。コードスニペットは次のとおりです。正確なジオフェンシングイベントを取得する方法

はまた、その後

void onLocationChanged(Location location) 

で、ジオフェンスを作成し、ジオフェンスに使用位置との間の距離を取得するために更新された位置を使用するように

lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 

を使用した後に気づいた、それもあるようです正確ではない。 onLocationChanged()はlocation#2を返し、distanceTo()は200m以上を返します。

また、デバイスが移動せずにテーブル上にある場合でも、onLocationChanged(Location location)はいつか別の場所を返します。

私はそれがプロバイダーによって異なるかもしれないが、テストは無線LANのカバーエリア内にあると思う。たぶんそれはどのように動作するのですか?

しかし、より正確または細かくする必要がある場合は、もっと正確なジオフェンスイベントまたは正確な現在の場所を取得する方法が異なるでしょうか?ジオフェンスが作成された後も、デバイスが移動せずにテーブルの上に置かれ

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); 
} 

答えて

0

GPS信号は、(あなたがそのような建物か何かの中にいる場合)お使いのデバイスのGPS信号が不良である場合は、別の振る舞いも、デバイスとの間で変化し、あなたはGPSの位置の周りにジャンプしていないことがわかります定数。 現在、100%のケースで正確な位置を取得するための良い方法はありません。

+0

ありがとうサギミモン!しかし、いくつかのアプリはLocationServicesでかなり信頼できると思われるので、いくらかの方法があるかもしれません。誰かが知っているかもしれないかと疑問に思います。 – lannyf

関連する問題