首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GeoFencing最佳结构

GeoFencing最佳结构
EN

Stack Overflow用户
提问于 2016-05-31 13:38:32
回答 1查看 472关注 0票数 1

我在工作地点和地球击剑。但我看到了几个问题。我认为我遗漏了一些东西,但实际上,我对Geo栅栏和位置服务(例如融合位置API )有很多困惑。

我在做什么

根据我的应用程序场景,我必须获得用户位置(我使用的是融合位置API),我还询问用户他的目的地是什么,让他说他在A点,他选择了地点F。现在我希望我的应用程序能够通知他他已经到达F点了。

问题和混乱:

  1. 我被通知(通过我的通知),我已经到达我的目的地,只有当我在我的应用程序。看起来我的位置并没有将google更新到我现在的位置。那么,如果应用程序近在咫尺,或者在后台,谷歌还能追踪到他,最好的方法是什么?
  2. 我所做的就是在用户的目的地周围创建地理围栏。现在我不知道google跟踪用户的方法是什么。?请你帮我澄清一下,在创建地理围栏之后,谷歌是如何追踪我们的?
  3. 我是否需要在服务中实现一些东西,以便如果我的应用程序处于后台,甚至我的应用程序已经关闭,那么我的服务应该继续运行吗?

我想我对我的问题很清楚,我想要什么。请分享你的观点,并告诉我谷歌如何跟踪我们时,我们地理围栏任何位置?

我跟随来创建地理位置。

请告诉我你的看法。

EN

回答 1

Stack Overflow用户

发布于 2016-06-22 12:37:26

您应该使用目标坐标(Place )注册一个GEOFENCE_TRANSITION_ENTERGEOFENCE_TRANSITION_DWELL地理位置。

在您的活动/片段onCreate中,您应该创建Api客户机:

代码语言:javascript
复制
mGoogleApiClient = new GoogleApiClient.Builder(this)
                                      .addConnectionCallbacks(this)
                                      .addOnConnectionFailedListener(this)
                                      .addApi(LocationServices.API)
                                      .build();

还记得连接/断开:

代码语言:javascript
复制
protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

然后,在onConnected中,您应该执行以下操作:

代码语言:javascript
复制
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient,
                                            geofenceRequest,
                                            pendingIntent)

您应该只添加一次Geofence。

其中geofenceRequest是使用GeofencingRequest.Builder构建的:

代码语言:javascript
复制
geofenceRequest = new GeofencingRequest.Builder().addGeofence(yourGeofence).build()

其中yourGeofence和pendingIntent:

代码语言:javascript
复制
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可能是这样的:

代码语言:javascript
复制
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
                }
            }
        }
    }
}

请记住在您的清单中注册此服务:

代码语言:javascript
复制
<service android:name=".GeofenceTransitionsIntentService"/>

即使应用程序关闭,GeofenceTransitionsIntentService.onHandleIntent()也会被调用。

希望能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37547385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档