gpt4 book ai didi

android - Fragment Android 中的 GoogleApiClient

转载 作者:太空狗 更新时间:2023-10-29 13:55:23 25 4
gpt4 key购买 nike

我尝试自学 Android。在我的应用程序中,我想使用 fragment 来显示谷歌地图,当用户打开我的应用程序时,我想使用 GoogleApiClient 获取当前位置我的 fragment 代码:

public class HomeFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

public interface comuticateParent {
public void sendMess(String text);
}

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;


public HomeFragment() {
// Required empty public constructor
}

public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

comuticateParent callback;
Button btn1;
TextView textView;
MapView mMapView;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
LocationRequest mLocationRequest;
private GoogleMap googleMap;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);

mMapView.onResume(); // needed to get the map to display immediately

try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
googleMap.setMyLocationEnabled(true);

mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Log.d("onCreateView", Boolean.toString(mGoogleApiClient.isConnected()));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mylocation, 13));
}
});
return rootView;
}

@Override
public void onStart() {
mGoogleApiClient.connect();
Log.d("ConnectonStart", "Connected ");
Log.d("ONstart", Boolean.toString(mGoogleApiClient.isConnected()));
super.onStart();
}

@Override
public void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}

@Override
public void onResume() {
mGoogleApiClient.connect();
super.onResume();
mMapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}

@Override
public void onConnected(Bundle bundle) {
Log.d("Connect", "Connected ");
Log.d("onConnected", Boolean.toString(mGoogleApiClient.isConnected()));
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("Connect", "failed ");
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
Activity activity;
if (context instanceof Activity) {
activity = (Activity) context;
callback = (comuticateParent) getActivity();
}
}

}

问题在这里:-登录方法 onCreateView 出现在登录方法 onconnected 之前,所以我无法获取 getLastLocation(),因为 googleAPIClient 尚未连接。

我在谷歌中进行了搜索,但我不知道如何修复它。请帮我!对不起,我的英语最好。

最佳答案

第一个问题登录方法onCreateView出现在登录方法onconnected之前,因为GoogleApiClient会访问Google API,验证应用程序配置,验证证书指纹等。它需要太多长处理。为避免阻塞 UI 线程,它将在另一个线程中执行并使用异步回调。

第二个问题我无法获取 getLastLocation() 因为 googleAPIClient 尚未连接,因为 FusedLocationApi 只会维护后台,因此您需要获取 GoogleMap 在后台。

在这里查看我的示例:https://gist.github.com/quydm/a458d908c4da2496672f83372304f417

关于android - Fragment Android 中的 GoogleApiClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40159132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com