Mapbox Android determine zoom level that contain all markers

泄露秘密 提交于 2019-12-06 18:51:54

问题


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 find the android version

[https://www.mapbox.com/mapbox.js/example/v1.0.0/markers-only-at-zoom-level/]


回答1:


With the latest SDK version the existing answer does not work anymore. Instead, use this:

LatLngBounds latLngBounds = new LatLngBounds.Builder()
  .include(new LatLng(<marker 1 latlng position>)) 
  .include(new LatLng(<marker 2 latlng position>)) 
  .build();

mapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50));

Thanks to the friendly Mapbox support for providing this answer :)




回答2:


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));


来源:https://stackoverflow.com/questions/26173010/mapbox-android-determine-zoom-level-that-contain-all-markers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!