限制该地区的用户可以去上的MapView [英] Restricting the area the user can go to on Mapview

查看:131
本文介绍了限制该地区的用户可以去上的MapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的图形页面(OSMDroid版)的定制版本。 我使用自定义的砖在它里面,我只希望用户能够查看在那里我有我的自定义瓷砖的区域。 有没有一种方法来设置边界纬度多头所以当他们平移地图,它不走过去,这些界限?

I am using a customised version of the mapview (OSMDroid version). I am using custom tiles within it and I only want the user to be able to view the area where I have my custom tiles. Is there a way to set the boundary lat longs so when they pan the map it doesn't go past these boundaries?

推荐答案

柜面它可以帮助任何人......

Incase it helps anyone....

我有那种我使用的解决方案,它的工作原理确定,但可以肯定是更好,因为地图上可以去升技太远关闭屏幕前跳回来了! 它使用纬度多头,工作在哪里的地图,我设置4个坐标,这是大致的4个角落,我发现它的作品更好,如果你稍微设置成地图,而完全角落的地图,然后我工作,如果在纬度多头已经完全离开屏幕。如果这样它会反弹吗中途回:

I have sort of a solution that I am using, it works ok, but could definitely be better as the map can go abit too far off the screen before it jumps back! It uses the lat longs and works out where the map is, I set 4 coordinates which are roughly the 4 corners of the map I have found it works better if you set them slightly into the map rather exactly the corners, I then work out if the lat longs have left the screen completely.. if so it will bounce it halfway back:

我推翻了图形窗口和地图的OnTouch事件

I overrode the mapview and the OnTouch event of the map

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

这几件事情我要指出,如果你使用的是该考虑:

A few things I should point out if you are considering using this:

  1. 在我作出任何guarentees,它会为你的情况,你应该只把它作为一个起点。
  2. 对于赤道以北(我认为)地区,由于我已经做了坐标中的方法它只会工作!
  3. 它工作于动作向上的触摸事件,所以需要在用户需要其手指从屏幕的点。这意味着,当在地图甩这是完全不正确的,因为我无法工作,因为这个,我关掉了一扔通过重写一扔事件,并没有做任何事情在它在哪里的地图停止了,..这确实让地图有点摇动!

如果任何人有任何更好的解决方案,也可以提高我的code欢迎!

If anyone has any better solutions or can improve my code please feel free!

这篇关于限制该地区的用户可以去上的MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆