Note that there are some explanatory texts on larger screens.

plurals
  1. POLimit Scrolling on offline maps, in Android
    text
    copied!<p>I got these piece of codes or patches from osmdroid, and I decided to ask for your help guys because i don't have the enough knowledge to combine these codes to come up with on a solution on my problem, Scrolling limit on an offline map. I searched across the web, and modified tutorials. Honestly I tried to modify these codes but i have not found any progress. Basically I have an offline map from mapnik, and a few overlays. I don't know where to properly place these set of codes. Your ideas and modification will be a great help and also helps me keep going with my project and I guess your answers will definitely help others with the same problem as mine in the future. I know this is to much. Thank you sirs for your time, and God Bless.</p> <pre><code>public void onCreate(Bundle savedInstanceState) { ... ... m_mapView = (MapView) findViewById(R.id.mapview); m_mapView.setTileSource(TileSourceFactory.MAPNIK); } </code></pre> <p>First: BoundingBox</p> <pre><code>BoundingBoxE6 bbox = new BoundingBoxE6(9.37398, 123.33761, 9.23948, 123.25035); this.setScrollableAreaLimit(bbox); </code></pre> <p>Second: LimitScrollToGeographicArea.patch</p> <pre><code>Index: MapView.java =================================================================== --- MapView.java (revision 944) +++ MapView.java (working copy) @@ -103,6 +103,8 @@ protected MapListener mListener; + protected Rect mScrollableAreaLimit; + // for speed (avoiding allocations) private final Matrix mMatrix = new Matrix(); private final MapTileProviderBase mTileProvider; @@ -505,6 +507,36 @@ mMapOverlay.setUseDataConnection(aMode); } + /** + * Set the map to limit it's scrollable view to the specified BoundingBoxE6. Note that, like + * North/South bounds limiting, this allows an overscroll of half the screen size. This means + * each border can be scrolled to the center of the screen. + * + * @param boundingBox + * A lat/long bounding box to limit scrolling to, or null to remove any scrolling + * limitations + */ + public void setScrollableAreaLimit(BoundingBoxE6 boundingBox) { + final int worldSize_2 = TileSystem.MapSize(MapViewConstants.MAXIMUM_ZOOMLEVEL) / 2; + + // Clear scrollable area limit if null passed. + if (boundingBox == null) { + mScrollableAreaLimit = null; + return; + } + + // Get NW/upper-left + final Point upperLeft = TileSystem.LatLongToPixelXY(boundingBox.getLatNorthE6() / 1E6, + boundingBox.getLonWestE6() / 1E6, MapViewConstants.MAXIMUM_ZOOMLEVEL, null); + upperLeft.offset(-worldSize_2, -worldSize_2); + + // Get SE/lower-right + final Point lowerRight = TileSystem.LatLongToPixelXY(boundingBox.getLatSouthE6() / 1E6, + boundingBox.getLonEastE6() / 1E6, MapViewConstants.MAXIMUM_ZOOMLEVEL, null); + lowerRight.offset(-worldSize_2, -worldSize_2); + mScrollableAreaLimit = new Rect(upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y); + } + // =========================================================== // Methods from SuperClass/Interfaces // =========================================================== @@ -772,10 +804,26 @@ //I am confused with these codes below, where should I declare it? Int x, y in the onCreate method? x += (worldSize_2 * 2); while (x &gt; worldSize_2) x -= (worldSize_2 * 2); - while (y &lt; -worldSize_2) - y += (worldSize_2 * 2); - while (y &gt; worldSize_2) - y -= (worldSize_2 * 2); + if (y &lt; -worldSize_2) + y = -worldSize_2; + if (y &gt; worldSize_2) + y = worldSize_2; + + if (mScrollableAreaLimit != null) { + final int zoomDiff = MapViewConstants.MAXIMUM_ZOOMLEVEL - getZoomLevel(); + final int minX = mScrollableAreaLimit.left &gt;&gt; zoomDiff; + final int minY = mScrollableAreaLimit.top &gt;&gt; zoomDiff; + final int maxX = mScrollableAreaLimit.right &gt;&gt; zoomDiff; + final int maxY = mScrollableAreaLimit.bottom &gt;&gt; zoomDiff; + if (x &lt; minX) + x = minX; + else if (x &gt; maxX) + x = maxX; + if (y &lt; minY) + y = minY; + else if (y &gt; maxY) + y = maxY; + } super.scrollTo(x, y); // do callback on listener </code></pre> <p>Another one:</p> <pre><code> scrollToMethod public void scrollTo(int x, int y) { int curZoomLevel = mZoomLevel; final int worldSize_2 = TileSystem.MapSize(curZoomLevel) / 2; Log.v("HELP", "Scrolling to X=" + x + " Y=" + y + " ZL=" + curZoomLevel + " - WW="+worldSize_2); while (x &lt; -worldSize_2) x += (worldSize_2 * 2); while (x &gt; worldSize_2) x -= (worldSize_2 * 2); if (y &lt; -worldSize_2) y = -worldSize_2; if (y &gt; worldSize_2) y = worldSize_2; if (mScrollableAreaLimit != null) { int targetZoomLevel = getZoomLevel(); final int zoomDiff = MapViewConstants.MAXIMUM_ZOOMLEVEL - targetZoomLevel; //final int zoomDiff = MapViewConstants.MAXIMUM_ZOOMLEVEL - mZoomLevel; final int minX = mScrollableAreaLimit.left &gt;&gt; zoomDiff; final int minY = mScrollableAreaLimit.top &gt;&gt; zoomDiff; final int maxX = mScrollableAreaLimit.right &gt;&gt; zoomDiff; final int maxY = mScrollableAreaLimit.bottom &gt;&gt; zoomDiff; Log.v("HELP", "Limit: minX=" + minX + " maxX=" + maxX + " minY=" + minY + " maxY=" + maxY + " ZL=" + curZoomLevel + " ZLTarget="+ targetZoomLevel + " ZD="+zoomDiff); if (x &lt; minX) { Log.v("HELP", "!!! X=" + x + " minX=" + minX + " CORRECTION:" + (minX-x)); x = minX; } else if (x &gt; maxX) { Log.v("HELP", "!!! X=" + x + " maxX=" + maxX + " CORRECTION:" + (maxX-x)); x = maxX; } if (y &lt; minY) { Log.v("HELP", "!!! Y=" + y + " minY=" + minY + " CORRECTION:" + (minY-y)); y = minY; } else if (y &gt; maxY) { Log.v("HELP", "!!! Y=" + y + " maxY=" + maxY + " CORRECTION:" + (maxY-y)); y = maxY; } } super.scrollTo(x, y); // do callback on listener if (mListener != null) { final ScrollEvent event = new ScrollEvent(this, x, y); mListener.onScroll(event); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload