Restricting the area the user can go to on Mapview

前端 未结 4 1900
陌清茗
陌清茗 2021-02-10 10:22

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

4条回答
  •  梦毁少年i
    2021-02-10 11:18

    Incase it helps anyone....

    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:

    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. I make no guarentees that it will work for your situation and you should only use it as a starting point.
    2. It will only work for areas north of the equator (I think) due to the way I've done the coords!
    3. It works on the "action up" of the touch event, so it takes the point where the user takes their finger off the screen. This means when the map flings it's completely inaccurate as I could not work out where the map stopped, because of this I turned off the fling by overriding the fling event and not doing anything in it.. this does make the map a bit jolty!

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

提交回复
热议问题