【发布时间】:2014-11-13 16:01:40
【问题描述】:
我正在试用 Google 的地理围栏示例项目。我正在正确获取进入/退出事件。但是我怎样才能从中获取用户的当前位置。即使在进入/退出点之后,我也想跟踪用户位置。请帮忙。 sample project is from the developer portal.
【问题讨论】:
标签: geolocation location latitude-longitude geofencing android-geofence
我正在试用 Google 的地理围栏示例项目。我正在正确获取进入/退出事件。但是我怎样才能从中获取用户的当前位置。即使在进入/退出点之后,我也想跟踪用户位置。请帮忙。 sample project is from the developer portal.
【问题讨论】:
标签: geolocation location latitude-longitude geofencing android-geofence
在您的广播接收器或服务中,您会获得一个 GeofencingEvent ,使用它来获取触发位置
public class GeofenceTransitionsIntentService extends IntentService {
//[...]
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
// ...
}
Location l = geofencingEvent.getTriggeringLocation()
}
//[...]
}
【讨论】:
您需要以正常方式从 Fused Location Provider 获取用户位置。完整教程在这里: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
【讨论】: