Search code examples
androidbaidubaidu-map

Handle orientation changes in baidu maps


What is the correct way to handle orientation changes in Baidu Maps?

There seems to be no getter or setter to get the center and zoom level or the bounds of the currently shown map.


Solution

  • The normal way to save the state is onSaveInstanceState(...) callback. I wrote this code here:

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("map", mapView.getMap().getMapStatus());
    }
    

    When you want to recover the data try this example (taken from a fragment):

    @Nullable
    @Override
    public synchronized View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.baido_map, container, false);
        mapView = (MapView) view.findViewById(R.id.baido_map);
    
        if(savedInstanceState != null) {
            mapView.getMap().setMapStatus(MapStatusUpdateFactory.newMapStatus(savedInstanceState.getParcelable("map")));
        }
    
        return view;
    }