限制用户可以在Mapview上访问的区域

Bex*_*Bex 5 android google-maps openstreetmap osmdroid

我正在使用mapview的自定义版本(OSMDroid版本).我在其中使用自定义图块,我只希望用户能够查看我有自定义图块的区域.有没有办法设置边界lat long,所以当它们平移地图时它不会超过这些边界?

kur*_*arc 6

更新:我知道这是一个老问题,但osmdroid现在有一个setScrollableAreaLimit()方法,可以实现你想要的.还有一个setMinZoomLevel()和setMaxZoomLevel()方法可以轻松限制缩放级别.

原始答案:

请密切注意:

http://code.google.com/p/osmdroid/issues/detail?id=209

已经创建了一个补丁,很快就会集成.


Bex*_*Bex 1

万一它对任何人有帮助......

我正在使用一种解决方案,它工作正常,但肯定会更好,因为地图在跳回来之前可能会离屏幕太远!它使用经纬度并计算出地图的位置,我设置了 4 个坐标,大致是地图的 4 个角,我发现如果将它们稍微设置到地图中而不是精确地设置角,效果会更好,然后我计算出是否纬度经度已完全离开屏幕..如果是这样,它会将其弹回一半:

我重写了地图视图和地图的 OnTouch 事件

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_UP) {

        // (only works for north of equator)
        // * map right side (lat) can't go past the left (lat) of screen

        // get geopoints of the 4 corners of the screen
        Projection proj = getProjection();
        GeoPoint screenTopLeft = proj.fromPixels(0, 0);
        GeoPoint screenTopRight = proj.fromPixels(getWidth(), 0);
        GeoPoint screenBottomLeft = proj.fromPixels(0, getHeight());

        double screenTopLat = screenTopLeft.getLatitudeE6() / 1E6;
        double screenBottomLat = screenBottomLeft.getLatitudeE6() / 1E6;
        double screenLeftlong = screenTopLeft.getLongitudeE6() / 1E6;
        double screenRightlong = screenTopRight.getLongitudeE6() / 1E6;

        double mapTopLat = BoundsTopLeftCorner.getLatitudeE6() / 1E6;
        double mapBottomLat = BoundsBottomLeftCorner.getLatitudeE6() / 1E6;
        double mapLeftlong = BoundsTopLeftCorner.getLongitudeE6() / 1E6;
        double mapRightlong = BoundsTopRightCorner.getLongitudeE6() / 1E6;

        // screen bottom greater than map top
        // screen top less than map bottom
        // screen right less than map left
        // screen left greater than map right
        boolean movedLeft = false;
        boolean movedRight = false;
        boolean movedUp = false;
        boolean movedDown = false;

        boolean offscreen = false;
        if (screenBottomLat > mapTopLat) {
            movedUp = true;
            offscreen = true;
        }
        if (screenTopLat < mapBottomLat) {
            movedDown = true;
            offscreen = true;
        }
        if (screenRightlong < mapLeftlong) {
            movedLeft = true;
            offscreen = true;
        }
        if (screenLeftlong > mapRightlong) {
            movedRight = true;
            offscreen = true;
        }

        if (offscreen) {
            // work out on which plane it's been moved off screen (lat/lng)

            if (movedLeft || movedRight) {

                double newBottomLat = screenBottomLat;
                double newTopLat = screenTopLat;

                double centralLat = newBottomLat
                        + ((newTopLat - newBottomLat) / 2);
                if (movedRight)
                    this.getController().setCenter(
                            new GeoPoint(centralLat, mapRightlong));
                else
                    this.getController().setCenter(
                            new GeoPoint(centralLat, mapLeftlong));

            }
            if (movedUp || movedDown) {

                // longs will all remain the same
                double newLeftLong = screenLeftlong;
                double newRightLong = screenRightlong;

                double centralLong = (newRightLong + newLeftLong) / 2;

                if (movedUp)
                    this.getController().setCenter(
                            new GeoPoint(mapTopLat, centralLong));

                else
                    this.getController().setCenter(
                            new GeoPoint(mapBottomLat, centralLong));
            }

        }

    }
    return super.onTouchEvent(ev);
}}
Run Code Online (Sandbox Code Playgroud)

如果您正在考虑使用此功能,我应该指出以下几点:

  1. 我不保证它适合您的情况,您只能将其用作起点。
  2. 由于我完成坐标的方式,它仅适用于赤道以北的地区(我认为)!
  3. 它适用于触摸事件的“向上操作”,因此它会记录用户将手指从屏幕上移开的点。这意味着当地图投掷时,它是完全不准确的,因为我无法弄清楚地图在哪里停止,因此我通过覆盖投掷事件而不在其中执行任何操作来关闭投掷。这确实使地图有点摇晃!

如果有人有更好的解决方案或可以改进我的代码,请随时!