Add bounds to map to avoid swiping outside a certain region

前端 未结 9 1136
抹茶落季
抹茶落季 2021-02-01 12:45

My app shows a map and i want that users can\'t swipe over a certain region. So i\'m trying to add bounds but it makes the app to crash. Here is the working code:



        
9条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 13:25

    This worked for me:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        mMapFragment.getMapAsync(this);
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMapFragment.getView().getViewTreeObserver().addOnGlobalLayoutListener(this);
    }
    
    @Override
    public void onGlobalLayout() {
        mMapFragment.getView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
        //Only after onMapReady & onGlobalLayout newLatLngBounds will be available
        initMapPosition(); //newLatLngBounds here
    }
    

提交回复
热议问题