【问题标题】:GoogleApiClient cannot connect in android studioGoogleApiClient 无法在 android studio 中连接
【发布时间】:2019-02-12 18:51:35
【问题描述】:

我有一段时间无法连接 google map Api 客户端。即使对象返回不为空,但仍然无法连接。 我还在清单中提到了权限

下面是我的代码

 public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        buildGoogleApiClient();
}

         protected synchronized void buildGoogleApiClient(){
        googleApiClient = new GoogleApiClient.Builder(getBaseContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        googleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(mLocationRequest.PRIORITY_HIGH_ACCURACY);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
   // 

LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest,new LocationCallback());
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
    }

我的 gradle 依赖项如下所示

 dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.google.android.gms:play-services-maps:15.0.1'
        implementation 'com.google.android.gms:play-services-vision:15.0.2'
        implementation 'com.google.android.gms:play-services-location:15.0.1'
        implementation 'com.google.android.gms:play-services-maps:15.0.1'

        implementation 'com.google.firebase:firebase-core:16.0.1'
        implementation 'com.google.firebase:firebase-database:16.0.1'
        implementation 'com.android.support:design:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    apply plugin: 'com.google.gms.google-services'

我是谷歌地图项目的新手,对它了解较少。如果有人有解决方案,请帮助我。提前致谢

【问题讨论】:

  • “无法连接”——有错误代码吗?
  • 检查你的国家是否在谷歌中被禁止同时检查清单文件中是否存在这两行
  • 并添加登录onMapReady()
  • 调用 buildGoogleApiClient();在 onCreate()
  • 没有错误代码先生。只是 isConnected() 返回 false。不推荐使用的 API 会出现任何问题:LocationServices.getFusedLocationProviderClient(this) 吗? @TheWanderer

标签: java android google-maps geolocation google-api-client


【解决方案1】:

我通过在我使用的公共类中实现GoogleApiClient.OnConnectionFailedListener 解决了这个问题。您可能还没有这样做,这是连接 API 所必需的。

【讨论】:

    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多