【问题标题】:OSMDroid Showing more than one maps and infinite scrollOSMDroid 显示不止一张地图和无限滚动
【发布时间】:2017-07-06 08:15:58
【问题描述】:

我有一个简单的 Android 应用程序。我正在尝试显示一个简单的地图(就像我们在谷歌地图中一样)。在OSMDroid 网站之后,我按照那里的说明设置了应用程序。

我有如下所示的 Manifest 文件:

<?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>

默认输出如下图:

问题: 当我向上/向下或横向滚动地图时,输出会变成这样:

如上图所示,屏幕上没有一张地图。当您滚动时,它们的数量是无限的,地图不断出现(水平和垂直滚动)。

我的要求是只有一个地图实例,就像我们在 Google Map API 中一样。那么如何使用 OSMDroid 实现同样的效果呢?

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

【问题讨论】:

    标签: android openstreetmap osmdroid


    【解决方案1】:

    我目前正在努力解决同样的问题。我找到了以下避免“重复”的解决方案(来自OSMDROID displays multiple maps with polylines in Android

    MapView map = (MapView) findViewById(R.id.map);
    map.setScrollableAreaLimitDouble(new BoundingBox(85, 180, -85, -180));
    map.setMaxZoomLevel(20.0);
    map.setMinZoomLevel(4.0);
    map.setHorizontalMapRepetitionEnabled(false);
    map.setVerticalMapRepetitionEnabled(false);
    map.setScrollableAreaLimitLatitude(MapView.getTileSystem().getMaxLatitude(), 
    MapView.getTileSystem().getMinLatitude(), 0);
    

    不完全是我想要的,但它可以解决你的问题。它使用 scai 提到的边界框。我会进一步看看是否有替代方案

    【讨论】:

      【解决方案2】:

      对我来说,下面的代码有效:

      map.isHorizontalMapRepetitionEnabled = false
      map.isVerticalMapRepetitionEnabled = false
      

      所以,除了你的代码,只需要在设置缩放值之前加上这两个参数

      【讨论】:

        【解决方案3】:

        首先,这仍然只是一张地图,因为我们的世界是圆的。

        其次,您可以在您的 MapView 对象上调用 setScrollableAreaLimit() 以将视图限制为特定的 BoundingBox

        【讨论】:

        • 感谢您的回答。但需要更多细节。单个地图占用的区域是多少(以便我可以设置可滚动区域)?世界是圆形/球形,但我们的表示是在平面屏幕上,所以我只需要一个地图实例。
        • 我没有尝试过,但我的猜测是指定一个边界框,lat 从&gt;-90&lt;90,lon 从&gt;-180&lt;180
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-20
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 2014-04-08
        相关资源
        最近更新 更多