如何从公共List <geofence> getTriggeringGeofences()中检索触发的地理围栏的纬度和经度

时间:2015-10-12 05:47:53

标签: android geofencing

我是Android新手,我正在开展一个地理围栏项目。

是否可以从公共列表getTriggeringGeofences()中检索触发的地理围栏的纬度和经度?

我希望在收到通知提醒并用户点击通知后在地图中显示已触发的位置。点击通知后,应显示所有触发的地理位置固定的mapActivity。 要添加标记,我需要触发位置的LatLong。我如何实现这一目标?

2 个答案:

答案 0 :(得分:0)

您可以从IntentService地理围栏事件中获取纬度和经度

GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

意图服务是触发地理围栏的地方,所以你将在这里添加引脚:

public class GeofenceTransitionsIntentService extends IntentService {

protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this,
                geofencingEvent.getErrorCode());
        Log.e(TAG, errorMessage);
        return;
    }

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        // Get the geofences that were triggered. A single event can trigger
        // multiple geofences.
        List triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(
                this,
                geofenceTransition,
                triggeringGeofences
        );

        // Send notification and log the transition details.
        sendNotification(geofenceTransitionDetails);
        Log.i(TAG, geofenceTransitionDetails);
    } else {
        // Log the error.
        Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
                geofenceTransition));
    }
}

Android官方文档有一个关于实现地理围栏的很好的教程:

https://developer.android.com/training/location/geofencing.html

答案 1 :(得分:0)

你可以从GeofencingEvent获得它。

protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        //TODO: Error handling

        return;
    }

    Location location = geofencingEvent.getTriggeringLocation();
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
}