Add bounds to map to avoid swiping outside a certain region

前端 未结 9 1158
抹茶落季
抹茶落季 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:34

    Documentation for onMapReady() clearly states that you have to wait for both onMapReadyCallback and ViewTreeObserver.OnGlobalLayoutListener:

    Note that this does not guarantee that the map has undergone layout. Therefore, the map's size may not have been determined by the time the callback method is called. If you need to know the dimensions or call a method in the API that needs to know the dimensions, get the map's View and register an ViewTreeObserver.OnGlobalLayoutListener as well.

    It is quite inconvenient, but one way of doing it would be by using RxJava. You could emit two observables, first one in onMapReady and second one in onGlobalLayout, then zip them together and do whatever you need to do. This way you can be certain that both callbacks were fired.

提交回复
热议问题