在Android中限制离线地图上的滚动

时间:2011-10-26 03:06:55

标签: android maps openstreetmap osmdroid

我从osmdroid那里获得了这些代码或补丁,我决定请求你的帮助,因为我没有足够的知识来组合这些代码来解决我的问题,滚动限制离线地图。我在网上搜索,并修改了教程。老实说,我试图修改这些代码,但我没有找到任何进展。基本上我有mapnik的离线地图和一些叠加。我不知道在哪里正确放置这些代码。您的想法和修改将是一个很大的帮助,也有助于我继续我的项目,我想你的答案肯定会帮助其他人与我的未来相同的问题。我知道这很重要。谢谢你的时间先生,上帝保佑。

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

首先:BoundingBox

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

第二名: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

另一个:

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

1 个答案:

答案 0 :(得分:7)

首先在终端上使用此命令:

svn checkout http://osmdroid.googlecode.com/svn/branches/release_3_0_5

将下载稳定版

然后按照此处导入您下载的文件夹的内容:

  

在Eclipse中,右键单击Package区域,然后选择以下内容:

click on Import...
select General -> Existing Projects into Workspace
click Next
click Browse...
select the checked out projects' directories
    osmdroid-android (import as a java project)
    OSMMapTilePackager (import as a java project)
    OpenStreetMapViewer (mport as an android project) 
click OK
click Finish
  1. 现在打开这个java文件 - &gt; osmdroid-机器人/ SRC /组织/ osmdroid /视图/ MapView.java
  2. 现在如此patch file中所述,修改MapView.java(添加 代码,无论何处+,删除代码 - )
  3. 同样修改MapView.java中的computeScroll(),如here
  4. 所述
  5. 现在,要使用此修改后的.java文件,您需要创建一个新jar 您可以在项目中包含的文件 这是一个循序渐进的过程to create jar

  6. 将新创建的jar文件添加到项目的构建路径中     r准备使用你的改良罐子

  7. 现在在您的活动类中使用它:

      

    BoundingBoxE6 bbox = new BoundingBoxE6(限制北,限制东,限制   南,限西;)       mapView.setScrollableAreaLimit(BBOX);