模拟GPS提供程序的地理围栏

时间:2019-07-01 08:33:44

标签: android gps geofencing android-geofence

我目前正在开发适用于Android的地理围栏应用程序,我遵循了他们的tutorial,但在使用模拟GPS定位应用程序时,似乎无法正确地触发BroadcastReceiver。

我还要启动定位服务,它每隔10000毫秒间隔就会从硬件获取位置。我完全遵循此Github repository

    private void populateGeofenceList() {
    for (Map.Entry<String, LatLng> entry : Constants.BAY_AREA_LANDMARKS.entrySet()) {

        geofenceArrayList.add(new Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this
                // geofence.
                .setRequestId(entry.getKey())

                // Set the circular region of this geofence.
                .setCircularRegion(
                        entry.getValue().latitude,
                        entry.getValue().longitude,
                        Constants.GEOFENCE_RADIUS_IN_METERS
                )

                // Set the expiration duration of the geofence. This geofence gets automatically
                // removed after this period of time.
                .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
                // Set the transition types of interest. Alerts are only generated for these
                // transition. We track entry and exit transitions in this sample.
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                        Geofence.GEOFENCE_TRANSITION_EXIT)
                .setLoiteringDelay(0)
                // Create the geofence.
                .build());
    }
}


    private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();

    // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
    // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
    // is already inside that geofence.
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);

    // Add the geofences to be monitored by geofencing service.
    builder.addGeofences(geofenceArrayList);


    // Return a GeofencingRequest.
    return builder.build();
}

1 个答案:

答案 0 :(得分:0)

一些建议:

  1. 检查位置适配器是否打开。
  2. 检查是否已授予位置permissions

  3. 检查经度和纬度值是否正确。 google maps是否显示正确的位置,即您期望触发的位置。

  4. 如果需要,设置停留时间(setLoiteringDelay(10000))。
  5. 您是否已设置BroadcastReceiver来接收documentation中提到的回调。