Add bounds to map to avoid swiping outside a certain region

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

    You can keep the error from occurring by following the advice from the log:

    "...use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions."

    The extra parameters are the width and height of your screen. Here's how your updated code would look:

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    builder.include(new LatLng(47.09194444, 18.52166666));
    builder.include(new LatLng(36.448311, 6.62555555));
    LatLngBounds bounds = builder.build();
    
    // begin new code:
    int width = getResources().getDisplayMetrics().widthPixels;
    int height = getResources().getDisplayMetrics().heightPixels;
    int padding = (int) (width * 0.12); // offset from edges of the map 12% of screen
    
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
    // end of new code
    
    map.animateCamera(cu);
    

提交回复
热议问题