Osmdroid最大缩放级别显示两个地理位置

时间:2017-08-01 10:09:38

标签: android geolocation maps osmdroid

我正在使用 osmdroid 库来渲染地图。我有两个间歇性变化的地理点,并希望org.osmdroid.views.MapView调整大小并进行翻译,确保两个点始终可见。我可以轻松地将地图重新​​定位在两者之间的中间位置,但如何设置缩放级别以确保我的两个点可见?

更新

  //Getting pickup and destination latitude and longitude
                GeoPoint pickupLocation = null;
                if (tripRequest.getPickupLat() != null && tripRequest.getPickupLong() != null) {
                    pickupLocation = new GeoPoint(tripRequest.getPickupLat(), tripRequest.getPickupLong());
                }
                GeoPoint destinationLocation = null;
                if (tripRequest.getDestinationLat() != null && tripRequest.getDestinationLong() != null) {
                    destinationLocation = new GeoPoint(tripRequest.getDestinationLat(), tripRequest.getDestinationLong());
                }

                if (destinationLocation != null && pickupLocation != null) {
                    //Adding destination marker to map
                    Marker destinationLocationMarker = new Marker(map);
                    destinationLocationMarker.setPosition(destinationLocation);
                    destinationLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
                    destinationLocationMarker.setIcon(context.getResources().getDrawable(R.mipmap.pin_green_small));
                    map.getOverlays().add(destinationLocationMarker);
                    //Adding pickup marker to map
                    Marker pickupLocationMarker = new Marker(map);
                    pickupLocationMarker.setPosition(pickupLocation);
                    pickupLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
                    pickupLocationMarker.setIcon(context.getResources().getDrawable(R.mipmap.pin_red_small));
                    map.getOverlays().add(pickupLocationMarker);


                    BoundingBox boundingBox = BoundingBox.fromGeoPoints(Arrays.asList(destinationLocation, pickupLocation));
                    mapController = map.getController();

                    mapController.setCenter(boundingBox.getCenter());

                    mapController.setZoom(10);// I need to set this zoom properly
                    touchListener = new TouchListener();
                    map.setOnTouchListener(touchListener);

2 个答案:

答案 0 :(得分:2)

使用zoomToBoundingBox。它应该工作,如果没有问题将在其他地方。

尝试:

map.zoomToBoundingBox(boundingBox, false);
map.invalidate();

这仍然无效,请检查此回复: zoom mapView to a certain bounding box on osmdroid

如果你真的想自己计算变焦,你可以随时在source code of zoomToBoundingBox method找到灵感并修改计算以满足你的需要。

答案 1 :(得分:0)

我相信你正在寻找mapview或地图控制器中的缩放范围。使用您的积分中的最大/最小纬度。您想要在日期行和赤道上进行测试