Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

android - No map shown in osmdroid

I tried to make an osmdroid project using Android Studio but any map is shown. I have just a blanck grid.

In my xml file I put the folowing code :

    <?xml
version="1.0" encoding="utf-8"
?>
<LinearLayout
xmlns:android="..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<org.osmdroid.views.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
tilesource="Mapnik"
/>
</LinearLayout>

in my .java file I made this onCreate method

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

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

IMapController mapController = mMap.getController();

mapController.setZoom(14);
mapController.setCenter(new GeoPoint(48.745, -3.455));
ScaleBarOverlay scala = new ScaleBarOverlay(mMap);
mMap.getOverlays().add(scala);
mMap.invalidate();

And I added the following permissions in the manifest:

<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"
/>

Do you know how can I solve this problem ?

I used osmdroid-android:5.6.4' (and I tried also other versions but I have the same problem with all)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...