第二次没有在片段内加载MapFragment。我试图在覆盖方法中更改一些代码,但它仍然无法正常工作。我正在实现这些接口
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener,
NavigationView.OnNavigationItemSelectedListener
这是代码
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_new1, container, false);
FragmentManager manager =getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
fragment = new SupportMapFragment();
transaction.replace(R.id.map, fragment);
transaction.commit();
shared = getActivity().getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// value_latLan=new ArrayList<>();
app_bar = (CoordinatorLayout) view.findViewById(R.id.app_bar);
layoutCartNoItems = (LinearLayout) view.findViewById(R.id.layout_cart_empty);
hv = (HorizontalScrollView) view.findViewById(R.id.hv);
l11 = (LinearLayout) view.findViewById(R.id.l1);
l2 = (LinearLayout) view.findViewById(R.id.l2);
// fragment.getView().setVisibility(View.GONE);
l11.setBackgroundColor(Color.parseColor("#dddddd"));
hv.setHorizontalScrollBarEnabled(false);
// app_bar.setVisibility(View.GONE);
// getActivity().getActionBar().hide();
l11.setVisibility(View.GONE);
l2.setVisibility(View.GONE);
layoutCartNoItems.setVisibility(View.VISIBLE);
// Toast.makeText(getContext().getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
fragment.setRetainInstance(true);
fragment.getMapAsync(this);
return view;
}
@Override
public void onPause() {
super.onPause();
//stop location updates when Activity is no longer active
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void onMapReady(GoogleMap googleMap)
{
mGoogleMap=googleMap;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Location Permission already granted
buildGoogleApiClient();
mGoogleMap.setMyLocationEnabled(true);
} else {
//Request Location Permission
checkLocationPermission();
}
}
else {
buildGoogleApiClient();
mGoogleMap.setMyLocationEnabled(true);
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
提前致谢..