java - GoogleApiClient 无法在 android studio 中连接

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

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

下面是我的代码

 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'

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

最佳答案

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

关于java - GoogleApiClient 无法在 android studio 中连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52222174/

相关文章:

Android:在选项卡内传输字符串/值,每个选项卡都有自己的 Activity (Getters-Setter 除外)

javascript - ng-click inside ui-gmap-windows 不工作

java - 在 Windows 中为 Ruby 和 RUN 设置 JAVA_HOME 环境变量

android - 将 View 放入具有自动换行的 Android 布局中

java - PHP 比较从 java 接收到的值

android - 在 Android 的 Activity 中嵌入一个 ListView

javascript - 为什么谷歌地图自动完成服务在结果中包含爱尔兰地址,即使仅限于 "gb"?

javascript - 在 Polymer Google Map 中缩放事件

java - 使用java编译python代码

java - IntelliJ IDEA 中的 TFS 团队项目设置?