首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >地理围栏和GoogleApiClient

地理围栏和GoogleApiClient
EN

Stack Overflow用户
提问于 2015-08-07 13:57:24
回答 1查看 1.1K关注 0票数 0

我正在为我的应用程序使用地理围栏,我遵循了声明为here的步骤,但每次我尝试初始化GoogleApiClient时,我都无法收到任何呼叫backs..following是我的代码。

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

我得到一个强制关闭错误

代码语言:javascript
复制
java.lang.IllegalStateException: GoogleApiClient is not connected yet.

onCreate()中的代码

代码语言:javascript
复制
geoFence = new Geofence.Builder()
            .setRequestId("DVSN")
            .setCircularRegion(18.5553785, 73.8164671, 10)
            .setExpirationDuration(43200000)
            .setTransitionTypes(
                    Geofence.GEOFENCE_TRANSITION_ENTER
                            | Geofence.GEOFENCE_TRANSITION_EXIT).build();
    LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            getGeofencingRequest(),
            getGeofencePendingIntent()
    ).setResultCallback(this);

私有方法

代码语言:javascript
复制
private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofence(geoFence);
    return builder.build();
}

private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent
    // back when
    // calling addGeofences() and removeGeofences().
    return PendingIntent.getService(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
}
EN

回答 1

Stack Overflow用户

发布于 2016-10-25 19:47:26

首先连接您的GoogleApiClient,然后将Geofence添加到GeofencingApi

代码语言:javascript
复制
mApiClient = new GoogleApiClient.Builder(mContext)
             .addApi(LocationServices.API)
             .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                  @Override
                  public void onConnected(Bundle bundle) {
                     Log.i(TAG, "Connected to GoogleApiClient");
                     addGeofencesOnLoad();
                  }
                  @Override
                  public void onConnectionSuspended(int i) {
                   }
              }).build();
mApiClient.connect();


public void addGeofencesOnLoad() {
    if (!mGoogleApiClient.isConnected()) {
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;
    }

 try {
    LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            getGeofencingRequest(),
            getGeofencePendingIntent()
    ).setResultCallback(this); // Result processed in onResult().
 } catch (SecurityException securityException) {
    logSecurityException(securityException);
 }
}

首先,使用Geofence.Builder创建geofence,为geofence设置所需的半径、持续时间和过渡类型。例如,要填充一个名为mGeofenceList的列表对象

代码语言:javascript
复制
mGeofenceList.add(new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this
// geofence.
.setRequestId(entry.getKey())

.setCircularRegion(
        entry.getValue().latitude,
        entry.getValue().longitude,
        Constants.GEOFENCE_RADIUS_IN_METERS
)
.setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
        Geofence.GEOFENCE_TRANSITION_EXIT)
.build());

以下代码段使用GeofencingRequest类及其嵌套的GeofencingRequestBuilder类指定要监视的geofence并设置相关geofence事件的触发方式

代码语言:javascript
复制
private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
}

以下代码片段显示了如何定义启动IntentServicePendingIntent

代码语言:javascript
复制
private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
    // calling addGeofences() and removeGeofences().
    return PendingIntent.getService(this, 0, intent, PendingIntent.
            FLAG_UPDATE_CURRENT);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31870666

复制
相关文章

相似问题

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