Jackson Curtis
Jackson Curtis

Reputation: 73

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)

Upvotes: 4

Views: 2335

Answers (1)

Mehran Mahmoudkhani
Mehran Mahmoudkhani

Reputation: 456

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);
    
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    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>

Upvotes: 2

Related Questions