2011-10-26 39 views
5

我從osmdroid得到了這些代碼或補丁,我決定請求你的幫助,因爲我沒有足夠的知識來組合這些代碼以便在解決我的問題,在離線地圖上滾動限制。我在網上搜索,並修改了教程。老實說,我試圖修改這些代碼,但我還沒有找到任何進展。基本上我有一張來自mapnik的離線地圖,以及一些覆蓋圖。我不知道在哪裏正確放置這些代碼。你的想法和修改將是一個很好的幫助,也可以幫助我繼續與我的項目,我想你的答案一定會幫助與我未來相同的問題的其他人。我知道這很重要。謝謝你的時間,上帝保佑。限制在離線地圖上滾動,在Android中

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

另外我有這方面的錯誤:超。scrollTo(x,y);如果(mListener!= null){ final ScrollEvent event = mListeiste.onScroll(event); mListener.onScroll(event); } – rahstame

回答

7

首先在終端上使用這個命令:

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

它會下載一個穩定的版本

然後按照這個導入ü下載該文件夾的內容:

在Eclipse中,包裝上的區域單擊右鍵,並選擇以下:

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文件 - > osmdroid-機器人/ SRC /組織/ osmdroid /視圖/ MapView.java
  2. 現在,作爲在此patch file所述,修改MapView.java(添加 代碼無論+,刪除代碼無論 - )
  3. 在MapView.java還修改computeScroll()如說here
  4. 現在,用這個修改的java文件,U需要創建一個新的jar文件 ,美可以在項目中包含 這裏是一個循序漸進的過程to create jar

  5. 這個新創建的jar文件添加到項目的構建路徑,你 讀準備使用您的改型罐

  6. 現在,在您的活動類使用:

    BoundingBoxE6 BBOX =新BoundingBoxE6(限北部,限制東,限制 南,限制西);
    mapView.setScrollableAreaLimit(bbox);