Ограничение прокрутки на автономных картах, в Android

Я получил эту часть кодов или патчей от osmdroid, и я решил попросить вашей помощи ребята, потому что у меня нет достаточно знаний, чтобы объединить эти коды, чтобы придумать решение моей проблемы, Прокрутка предела на автономной карте. Я искал в Интернете и модифицировал учебники. Честно говоря, я пытался изменить эти коды, но не нашел никакого прогресса. В основном у меня есть автономная карта из mapnik, и несколько наложений. Я не знаю, где правильно разместить этот набор кодов. Ваши идеи и модификации будут большой помощью, а также помогут мне продолжать мой проект, и я думаю, что ваши ответы определенно помогут другим с такой же проблемой, как у меня в будущем. Я знаю, что это слишком много. Спасибо вам, господа, за ваше время, и да благословит вас Бог.

public void onCreate(Bundle savedInstanceState) {
        ...
        ...
        m_mapView = (MapView) findViewById(R.id.mapview);
        m_mapView.setTileSource(TileSourceFactory.MAPNIK);
    }

First: BoundingBox

BoundingBoxE6 bbox = new BoundingBoxE6(9.37398, 123.33761, 9.23948, 123.25035);
this.setScrollableAreaLimit(bbox);

Second: LimitScrollToGeographicArea.patch

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 > worldSize_2)
            x -= (worldSize_2 * 2);
-       while (y < -worldSize_2)
-           y += (worldSize_2 * 2);
-       while (y > worldSize_2)
-           y -= (worldSize_2 * 2);
+       if (y < -worldSize_2)
+           y = -worldSize_2;
+       if (y > worldSize_2)
+           y = worldSize_2;
+
+       if (mScrollableAreaLimit != null) {
+           final int zoomDiff = MapViewConstants.MAXIMUM_ZOOMLEVEL - getZoomLevel();
+           final int minX = mScrollableAreaLimit.left >> zoomDiff;
+           final int minY = mScrollableAreaLimit.top >> zoomDiff;
+           final int maxX = mScrollableAreaLimit.right >> zoomDiff;
+           final int maxY = mScrollableAreaLimit.bottom >> zoomDiff;
+           if (x < minX)
+               x = minX;
+           else if (x > maxX)
+               x = maxX;
+           if (y < minY)
+               y = minY;
+           else if (y > maxY)
+               y = maxY;
+       }
        super.scrollTo(x, y);

        // do callback on listener

Another one:

 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 < -worldSize_2)
            x += (worldSize_2 * 2);
        while (x > worldSize_2)
            x -= (worldSize_2 * 2);
        if (y < -worldSize_2)
            y = -worldSize_2;
        if (y > 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 >> zoomDiff;
                final int minY = mScrollableAreaLimit.top >> zoomDiff;
                final int maxX = mScrollableAreaLimit.right >> zoomDiff;
                final int maxY = mScrollableAreaLimit.bottom >> zoomDiff;

                Log.v("HELP", "Limit: minX=" + minX + " maxX=" + maxX + " minY=" + minY + " maxY=" + maxY + " ZL=" + curZoomLevel + " ZLTarget="+ targetZoomLevel + " ZD="+zoomDiff);

                if (x < minX) {
                    Log.v("HELP", "!!! X=" + x + " minX=" + minX + " CORRECTION:" + (minX-x));
                    x = minX;
                } else if (x > maxX) {
                    Log.v("HELP", "!!! X=" + x + " maxX=" + maxX + " CORRECTION:" + (maxX-x));
                    x = maxX;
                }

                if (y < minY) {
                    Log.v("HELP", "!!! Y=" + y + " minY=" + minY + " CORRECTION:" + (minY-y));
                    y = minY;
                } else if (y > 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);
        }
    }
5
задан Pratik Bhat 18 January 2012 в 12:25
поделиться