【问题标题】:Android: Skobbler, how to limit map view in an area?Android:Skobbler,如何限制一个区域的地图视图?
【发布时间】:2015-09-30 17:41:56
【问题描述】:

我不仅是 Skobbler 的新手,也是 Map 的新手。目前,我正在尝试创建一个仅显示一个区域的 mapp 应用程序。

而且,我想要的是:

  • 用户无法离开该区域。
  • 请勿在该区域之外加载任何内容(标题、..)。
  • 用户只能查看和放大。

我尝试了像下面这样的绑定,但它不起作用:

SKBoundingBox boundingBox = new SKBoundingBox(47.087426, 8.257230, 46.874277, 8.637632);
mapView.fitBoundingBox(boundingBox, 0, 0);

请给点提示好吗?

【问题讨论】:

标签: android skmaps


【解决方案1】:

目前 SDK 不支持将地图操作限制在边界框内。

作为一种解决方法,只要屏幕上可见的地图区域发生变化(由于平移、缩放或旋转)并且超出了定义的边界框,地图就会切换回其内部的最后一个可见区域那个边界框:

public class MapActivity extends Activity implements SKMapSurfaceListener, ... { 

... 
// the box inside which map operations are allowed 
private SKBoundingBox box = new SKBoundingBox(47.087426, 8.257230, 46.874277, 8.637632); 

// last region that was visible on the screen and was inside the box 
private SKCoordinateRegion lastValidRegion = null; 

... 

@Override 
    public void onSurfaceCreated(SKMapViewHolder mapHolder) { 
... 
// position the map somewhere inside your box 
mapView.centerMapOnPosition(new SKCoordinate(8.304354, 47.050253)); 
} 

... 

// checks if a given region is inside the bounding box 
private boolean isInBoundingBox(SKCoordinateRegion newRegion) { 
        SKBoundingBox newBoundingBox = mapView.getBoundingBoxForRegion(newRegion); 
        if (newBoundingBox.getTopLeftLatitude() > box.getTopLeftLatitude() || newBoundingBox.getBottomRightLatitude() < box.getBottomRightLatitude() || 
                newBoundingBox.getTopLeftLongitude() < box.getTopLeftLongitude() || newBoundingBox.getBottomRightLongitude() > box.getBottomRightLongitude()) { 
            return false; 
        } 
        return true; 
    } 

@Override 
    public void onMapRegionChanged(SKCoordinateRegion mapRegion) { 
        boolean inBoundingBox = isInBoundingBox(mapRegion); 
        if (inBoundingBox) { 
// if mapRegion is valid save it 
            if (lastValidRegion == null) { 
                lastValidRegion = new SKCoordinateRegion(mapRegion.getCenter(), mapRegion.getZoomLevel()); 
            } else { 
                lastValidRegion.setCenter(mapRegion.getCenter()); 
                lastValidRegion.setZoomLevel(mapRegion.getZoomLevel()); 
            } 
        } else { 
// if mapRegion is invalid reposition the map inside the bounding box 
            if (lastValidRegion != null) { 
                mapView.changeMapVisibleRegion(lastValidRegion, false); 
            } 
        } 
    } 

} 

此外,如果需要,还可以使用 SKMapSettings.setZoomLimits(...) 方法将限制应用于缩放级别。

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 2015-11-25
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多