片段中的 FusedLocationApi MapView - addConnectionCallbacks、onConnectionSuspended

FusedLocationApi MapView in Fragment - addConnectionCallbacks, onConnectionSuspended

我在一个片段中有一个 MapView,我遇到了两个似乎找不到解决方案的问题。我已经搜索过,但我没有看到其他人遇到这些问题。作为参考,我主要关注开发者页面上的 this 教程。

这是我的片段:

public class MapFragment extends Fragment
        implements GooglePlayServicesClient.ConnectionCallbacks,
                   GooglePlayServicesClient.OnConnectionFailedListener,
                   LocationListener

第一个问题在这里:

 mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)  //problem!
            .addOnConnectionFailedListener(this) //problem!
            .build();

调用 addConnectionCallbacks(this) 会报错,提示无法应用于 myPackageName.MapFragment。我知道你必须在这里传递一个 GoogleApiClient.ConnectionCallbacks 侦听器,但我看到的每个示例都使用 this,我不确定该怎么做。同样的问题出现在addOnConnectionFailedListener。在我的片段中,我已经实现了所有必要的方法,例如 onLocationChanged().

第二个问题在这里:

@Override
public void onConnectionSuspended(int i)
{
    Log.i(TAG_MAP_FRAGMENT, "GoogleApiClient connection has been suspended");
}

这会给出一条错误消息:Method does not override method from its superclass。我已经搜索过,但找不到遇到此问题的其他人。我不确定如何处理它。

有人知道如何解决这些问题吗?感谢您的帮助!

而不是:

implements GooglePlayServicesClient.ConnectionCallbacks,
           GooglePlayServicesClient.OnConnectionFailedListener

尝试:

implements GoogleApiClient.ConnectionCallbacks,
           GoogleApiClient.OnConnectionFailedListener

这解决了上面最初发布的所有三个问题。 但是,现在方法onDisconnected() 被标记为错误method does not override method from superclass

当您使用所有四个时错误已修复:

implements GooglePlayServicesClient.ConnectionCallbacks,
           GooglePlayServicesClient.OnConnectionFailedListener,
           GoogleApiClient.ConnectionCallbacks,
           GoogleApiClient.OnConnectionFailedListener

这看起来并不理想,但它确实有效,至少目前如此。如果您偶然发现这个问题并且知道更好的解决方法,请留下回复。