Restricting the area the user can go to on Mapview

前端 未结 4 1888
陌清茗
陌清茗 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条回答
  • 2021-02-10 11:09

    Update: I know this is an old question, but osmdroid now has a setScrollableAreaLimit() method that will achieve what you are looking for. There is also a setMinZoomLevel() and setMaxZoomLevel() method to easily restrict zoom levels.

    Original answer:

    Please keep an eye on:

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

    A patch has already been created, and will likely be integrated shortly.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-10 11:19

    I'm looking for exactly the same thing.

    My best lead is to add an Overlay, which extends boolean onScroll(...). If this returns true, then the scroll is cancelled.

    This is exactly how I want it, except for one thing: flinging/flicking. The same approach can be used to cancel fling events, though you only get to hear about it at the start of the fling.

    Ideally, you'd be able to listen to the computeScroll() method, and limit the (x, y) of the scroll, based on mScroller.getCurX() and mScroller.getCurY().

    0 讨论(0)
  • 2021-02-10 11:23

    This is cross-posted from the osmdroid thread. There's now lists a BoundedMapView class, which implements the afortementioned patch.

    For those of use using the .jar or otherwise not that familiar with patches, I cobbled together a subclass of MapView that supports limiting the user's view to a specific area.

    Details on how to use it, if not obvious, can be found at http://www.sieswerda.net/2012/08/15/boundedmapview-a-mapview-with-limits/

    0 讨论(0)
提交回复
热议问题