我已经和这个问题坐了很长时间了。我有4个选项卡(ViewPager)第二个选项卡包含一个MapView。当我从第一个或第三个选项卡导航到地图选项卡时,地图显示正常,它会缩放到我的位置并显示标记,但是一旦我从第四个选项卡导航到地图选项卡,它就不会设置。它只显示没有标记或没有缩放的空白地图。当我从标签中打开另一个活动并返回地图时,它也没有设置地图。
任何帮助都将受到赞赏,因为这个问题困扰了我好几个星期:
代码:
MapsAtivity.java
public class MapsActivity extends Fragment implements View.OnClickListener {
private static View view;
static LatLng p1 = null;
private static GoogleMap mMap;
private static String EVENT_LOC_URL = "";
public static Marker me;
public static String names[];
View v;
static Context ctx;
MapView mapView;
public static MapsActivity newInstance(int page, String title) {
MapsActivity mapsActivity = new MapsActivity();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
mapsActivity.setArguments(args);
return mapsActivity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
ctx = getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_maps, container, false);
mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
ctx = getActivity();
setUpMapIfNeeded();
EVENT_LOC_URL = getResources().getString(R.string.server) + "eventLocations.php";
FloatingActionButton mLocate = (FloatingActionButton)view.findViewById(R.id.locateme);
mLocate.setOnClickListener(this);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUpMapIfNeeded();
if (mMap != null)
setUpMap();
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
setUpMapIfNeeded();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
}
/***** Sets up the map if it is possible to do so *****/
public void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = mapView.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
}
private void setUpMap() {
MapsInitializer.initialize(this.getActivity());
getLongLat gl = new getLongLat(ctx);
showMyLoc(gl.getLatitude(), gl.getLongitude(), 16);
me = mMap.addMarker(new MarkerOptions()
.position(new LatLng(gl.getLatitude(), gl.getLongitude()))
.title("Current Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.locateme:
doLocate();
}
}
@Override
public void onDestroyView(){
super.onDestroyView();
// Remove the map fragment to prevent errors on the next load
if(mMap != null){
try {
Fragment mapFragment = new SupportMapFragment();
getChildFragmentManager()
.beginTransaction()
.remove(mapFragment)
.commit();
mMap = null;
} catch (Exception e){}
}
}
public void doLocate(){
me.remove();
MapsInitializer.initialize(this.getActivity());
getLongLat gl = new getLongLat(ctx);
showMyLoc(gl.getLatitude(), gl.getLongitude(), 16);
me = mMap.addMarker(new MarkerOptions()
.position(new LatLng(gl.getLatitude(), gl.getLongitude()))
.title("Current Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}
private static void showMyLoc(double latitude, double longitude, int i) {
/////----------------------------------Zooming camera to position user-----------------
LocationManager locationManager = (LocationManager) ctx.getSystemService(Activity.LOCATION_SERVICE);
Criteria criteria = new Criteria();
locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
try {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(latitude, longitude), 800));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)) // Sets the center of the map to location user
.zoom(i) // Sets the zoom
.bearing(0) // Sets the orientation of the camera to east
.tilt(90) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
} catch (Exception ex) {
Log.d("Error: ", ex.toString());
Toast.makeText(ctx, ctx.getString(R.string.error_loading_location), Toast.LENGTH_LONG).show();
}
}
没有logcat错误或任何我只收到空地图。
答案 0 :(得分:0)
我设法通过设置 offscreenpagelimit 并将以下代码添加到 onDestroyView 方法来解决此问题:
@Override
public void onDestroyView(){
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commitAllowingStateLoss();
}
在 MainActivity.java 中设置了 ViewPager 。我添加了以下代码:
viewPager.setOffscreenPageLimit(11);