Mapbox Android determine zoom level that contain all markers

前端 未结 2 1909
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 13:33

Is there a way to determine a zoom level so that all of my markers can fit in the zoom level? i am using mapbox 0.4.0

I think the answer is similar to this, but I can\'t

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-19 14:04

    okay I've figure it out. I need to create a bounding box containing all the markers

    final BoundingBox zoomLevel = findZoomLevel(hotelLocation,poiLocations);
    mv.zoomToBoundingBox(zoomLevel,true,true);
    .....
    
    private BoundingBox findZoomLevel(LatLng hotelLocation, LatLng[] poiLocations) {
            double bottomPadding = 0.002;
            double topPadding = 0.005; 
            BoundingBox box = new BoundingBox(findTopMost(hotelLocation,poiLocations).getLatitude() + topPadding,
                    findRightMost(hotelLocation,poiLocations).getLongitude(),
                    findBottomMost(hotelLocation,poiLocations).getLatitude() - bottomPadding,
                    findLeftMost(hotelLocation,poiLocations).getLongitude());
    
            return box;
    }
    

    Update

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(lat1,lng1)) 
      .include(new LatLng(lat2,lng2)) 
      .build();
    
    mapboxMap.moveCamera(
    CameraUpdateFactory.newLatLngBounds(LatLngBounds bounds, 
                                        int paddingLeft, 
                                        int paddingTop, 
                                        int paddingRight, 
                                        int paddingBottom));
    

提交回复
热议问题