Add bounds to map to avoid swiping outside a certain region

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

    Place the call within a runnable and post it to the views handler, like so:

        mapFragment.getView().post(new Runnable() {
            @Override
            public void run() {
                map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), CAMERA_PADDING));
            }
        });
    

    When the view has been laid out the runnable will execute and correctly position the map. The issue with ahmadalibalochs answer, is that you will see a flash of the globe before it correctly positions itself. Posting it to the view handler, resolves this issue.

提交回复
热议问题