从geo fence示例项目中获取当前位置

时间:2014-09-19 06:13:04

标签: geolocation location latitude-longitude geofencing android-geofence

我正在尝试Google的Geo fence示例项目。我正确地得到了进入/退出事件。但是如何从中获取用户的当前位置。我想跟踪进入/退出点后的用户位置。请帮忙。 sample project is from the developer portal.

2 个答案:

答案 0 :(得分:3)

在你的broadcastrecievero或服务中你得到一个GeofencingEvent,用它来获得触发位置

public class GeofenceTransitionsIntentService extends IntentService {

    //[...]

    @Override
    protected void onHandleIntent(Intent intent) {
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            // ...
        }

        Location l = geofencingEvent.getTriggeringLocation()

    }
    //[...]
}

答案 1 :(得分:0)

您需要以正常方式从融合位置提供商处获取用户位置。完整的教程在这里:http://developer.android.com/training/location/retrieve-current.html

但基本上,当您在Receiver或Service中收到地理围栏事件时,在其中获取LocationClient的实例,连接到该实例,并在回调中onConnected()获取最后一个已知位置:

Location position = locationClient.getLastLocation();

99.9%的时间,或者更好一点,您会发现这个位置在您的地理围栏内。

要继续跟踪用户位置更新,请按照接收位置更新:http://developer.android.com/training/location/receive-location-updates.html

进行操作
相关问题