2017-07-06 49 views
0

我有一個簡單的Android應用程序。我試圖展示一個簡單的地圖(就像我們在Google地圖中一樣)。在OSMDroid網站之後,我按照指示安裝了應用程序。OSMDroid顯示多個地圖和無限滾動

我有清單文件,如下圖所示:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="sample.osmdroid.com.osmdroidsample"> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我有如下圖所示我MainActivity.java

public class MainActivity extends AppCompatActivity { 

    ArrayList<OverlayItem> anotherOverlayItemArray; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID); 

     //important! set your user agent to prevent getting banned from the osm servers 
     Configuration.getInstance().load(getApplicationContext(), PreferenceManager.getDefaultSharedPreferences(getApplicationContext())); 
     setContentView(R.layout.activity_main); 

     MapView map = (MapView) findViewById(R.id.map); 
     map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE); 

     map.setBuiltInZoomControls(true); 
     map.setMultiTouchControls(true); 


     IMapController mapController = map.getController(); 
     mapController.setZoom(2); 

     map.setMaxZoomLevel(3); 
     map.setMinZoomLevel(2); 

    } 

    @Override 
    protected void onRestart() { 
     super.onRestart(); 
     //this will refresh the osmdroid configuration on resuming. 
     //if you make changes to the configuration, use 
     //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     //Configuration.getInstance().save(this, prefs); 
     Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this)); 
    } 
} 

相應的佈局文件activity_main.xml如下圖所示:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="sample.osmdroid.com.osmdroidsample.MainActivity"> 

    <TextView 
     android:id="@+id/myTxt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" /> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_below="@id/myTxt" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <org.osmdroid.views.MapView android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
</RelativeLayout> 

默認輸出如下所示:

enter image description here

問題: 當我滾動地圖向上/向下或橫盤整理,該出把原來是這樣的:

enter image description here

正如您上面的圖片出現在看不是屏幕中的單個地圖。當你滾動時,它們的數量是無限的,地圖不停地滾動(水平和垂直滾動)。

我的要求是像我們在Google Map API中那樣只有一個地圖實例。那麼如何使用OSMDroid實現相同?

P.S. I cannot use Google Maps API as the same is not supported in China. 

回答

0

首先,由於我們的世界是圓的,這仍然只是一張地圖。其次,您可以在您的MapView對象上調用setScrollableAreaLimit()以將視圖限制爲特定的BoundingBox

+0

感謝您的答覆。但需要更多細節。單個地圖佔用的區域是什麼(以便我可以設置可滾動區域)?世界是圓形/球形的,但我們的表示是在一個平面屏幕上,所以我只需要一個地圖實例。 – Zax

+0

我還沒有嘗試過,但我的猜測是指定一個從'> -90'到'<90'和從'> -180'到'<180'的經緯度的邊界框。 – scai