我在工作地点和地球击剑。但我看到了几个问题。我认为我遗漏了一些东西,但实际上,我对Geo栅栏和位置服务(例如融合位置API )有很多困惑。
我在做什么
根据我的应用程序场景,我必须获得用户位置(我使用的是融合位置API),我还询问用户他的目的地是什么,让他说他在A点,他选择了地点F。现在我希望我的应用程序能够通知他他已经到达F点了。
问题和混乱:
我想我对我的问题很清楚,我想要什么。请分享你的观点,并告诉我谷歌如何跟踪我们时,我们地理围栏任何位置?
我跟随这来创建地理位置。
请告诉我你的看法。
发布于 2016-06-22 12:37:26
您应该使用目标坐标(Place )注册一个GEOFENCE_TRANSITION_ENTER或GEOFENCE_TRANSITION_DWELL地理位置。
在您的活动/片段onCreate中,您应该创建Api客户机:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();还记得连接/断开:
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}然后,在onConnected中,您应该执行以下操作:
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient,
geofenceRequest,
pendingIntent)您应该只添加一次Geofence。
其中geofenceRequest是使用GeofencingRequest.Builder构建的:
geofenceRequest = new GeofencingRequest.Builder().addGeofence(yourGeofence).build()其中yourGeofence和pendingIntent:
yourGeofence = new Geofence.Builder()....build(); // Here you have to set the coordinate of Place F and GEOFENCE_TRANSITION_ENTER/GEOFENCE_TRANSITION_DWELL
pendingIntent = PendingIntent.getService(this,
(int)(System.currentTimeMillis()/1000),
new Intent(this, GeofenceTransitionsIntentService.class),
PendingIntent.FLAG_UPDATE_CURRENT);其中GeofenceTransitionsIntentService可能是这样的:
public class GeofenceTransitionsIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (!geofencingEvent.hasError()) {
int geofenceTransition = geofencingEvent.getGeofenceTransition();
if (geofenceTransition != -1) {
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
if (triggeringGeofences != null && triggeringGeofences.size() > 0) {
Geofence geofence = triggeringGeofences.get(0);
// Do something with the geofence, e.g. show a notification using NotificationCompat.Builder
}
}
}
}
}请记住在您的清单中注册此服务:
<service android:name=".GeofenceTransitionsIntentService"/>即使应用程序关闭,GeofenceTransitionsIntentService.onHandleIntent()也会被调用。
希望能帮上忙。
https://stackoverflow.com/questions/37547385
复制相似问题