No map shown in osmdroid

雨燕双飞 提交于 2019-11-29 17:07:20

use 6.1 version and up for osmdroid lib. follow these steps:

1. remove tilesource attribute from your xml file

tilesource="mapnik"  

2. put this snippet code before you call findviewbyId() your map.

final Context ctx = getApplicationContext();
      Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
      Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID);
      mMap = (MapView) findViewById(R.id.map);

3. add below snnipet after findViewbyId()

mMap.setTileSource(TileSourceFactory.MAPNIK); 

your final code should be like this :

activity.java

    private MapView mMap;
        @Override
        protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    final Context ctx = getApplicationContext();
          Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
          Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID);

    mMap = (MapView) findViewById(R.id.map);
    mMap.setTileSource(TileSourceFactory.MAPNIK); 
    mMap.setMultiTouchControls(true);
    mMap.setBuiltInZoomControls(true);
    }

.xml file

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <org.osmdroid.views.MapView android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!