Robin
Robin

Reputation: 835

OSMDroid - Map does not display

I am trying to load open street maps on my app.

My code is the following:

import android.app.Activity;
import android.os.Bundle;

import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.views.MapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;


public class OsmActivity extends Activity {
private MapView         mMapView;
private MapController   mMapController;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_osm);


    Configuration.getInstance().setUserAgentValue(getPackageName());

    mMapView = (MapView) findViewById(R.id.map);
    mMapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
    mMapView.setBuiltInZoomControls(true);
    mMapController = (MapController) mMapView.getController();
    mMapController.setZoom(13);
    GeoPoint gPt = new GeoPoint(51500000, -150000);
    mMapController.setCenter(gPt);
}
}

I am using osmdroid 5.6.5 (via Maven).

I define my user agent according to the osmdroid doc.

Configuration.getInstance().setUserAgentValue(getPackageName());

I have these API settings:

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.loco_zero.locolizetestsosm"
    minSdkVersion 21
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

With these permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"  />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

I have the grid and the zoom buttons, but the map won't show. I get these errors:

I/StorageUtils: /sdcard is NOT writable
I/StorageUtils: /storage/emulated/0 is NOT writable
I/OsmDroid: Using tile source: Mapnik
E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
E/SQLiteLog: (14) os_unix.c:31278: (13) open(/storage/emulated/0/osmdroid/tiles/cache.db) - 
E/SQLiteDatabase: Failed to open database '/storage/emulated/0/osmdroid/tiles/cache.db'.
              android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

D/OsmDroid: Unable to store cached tile from Mapnik /13/4090/2719, database not available.

What am I doing wrong? Can I tell OSMDroid to use something other than the sdcard folder?

EDIT

I added the following to get the permissions at runtime, it did the trick:

    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG, "Permission is granted");
        } else {
            Log.v(TAG, "Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

Upvotes: 1

Views: 8485

Answers (6)

Ahmadreza Gholizade
Ahmadreza Gholizade

Reputation: 1

add this // This line sets the user agent

Configuration.getInstance().setUserAgentValue("com.example.app_name/1.0 (My app_name App)");

// Explanation of the format:

  • "com.example.app_name": This is your app's package name (usually found in your manifest file).
  • "1.0": This is your app's version number.
  • "(My app_name App)": This is a descriptive name for your app (optional).

Upvotes: 0

tony gil
tony gil

Reputation: 9554

The accepted answer by @shane pointed me in the correct direction. But, permissions requests changed:

If migrating from pre-29 versions, you must also include legacy permissions to external storage <application android:requestLegacyExternalStorage="true" ...

Upvotes: 2

Suraj Bahadur
Suraj Bahadur

Reputation: 3928

I have implemented below OpenStreetMap in kotlin platform and it works.

Add below line build.gradle(app level)

implementation 'org.osmdroid:osmdroid-android:6.0.0'

Create LocationFragment.kt and paste the below code.

import android.graphics.BitmapFactory
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.app.base.BuildConfig
import com.app.base.base_classes.BaseFragment
import com.google.android.gms.maps.GoogleMap
import org.osmdroid.api.IMapController
import org.osmdroid.config.Configuration
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay

//Replace BaseFragment() with Fragment() class
class LocationFragment : BaseFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val view = inflater.inflate(com.app.base.R.layout.fragment_location, container, false)
        val ctx = activity!!.applicationContext
        Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
        Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID);
        val map = view.findViewById<MapView>(com.app.base.R.id.mapview)
        map.setUseDataConnection(true)
        //val map = view.findViewById(R.id.map) as MapView
        map.setTileSource(TileSourceFactory.MAPNIK)
        //map.setBuiltInZoomControls(true) //Map ZoomIn/ZoomOut Button Visibility
        map.setMultiTouchControls(true)
        val mapController: IMapController
        mapController = map.getController()

        //mapController.zoomTo(14, 1)
        mapController.setZoom(14)

        val mGpsMyLocationProvider = GpsMyLocationProvider(activity)
        val mLocationOverlay = MyLocationNewOverlay(mGpsMyLocationProvider, map)
        mLocationOverlay.enableMyLocation()
        mLocationOverlay.enableFollowLocation()

        val icon = BitmapFactory.decodeResource(resources, com.app.base.R.drawable.ic_menu_compass)
        mLocationOverlay.setPersonIcon(icon)
        map.getOverlays().add(mLocationOverlay)

        mLocationOverlay.runOnFirstFix {
            map.getOverlays().clear()
            map.getOverlays().add(mLocationOverlay)
            mapController.animateTo(mLocationOverlay.myLocation)
        }
        return view
    }

}

Use the below code in xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    tools:context="com.app.base.ui.location.LocationFragment">

    <org.osmdroid.views.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>

Upvotes: 0

jignyasa tandel
jignyasa tandel

Reputation: 271

add below line before this line setContentView(R.layout.activity_osm);

IConfigurationProvider provider = Configuration.getInstance();
provider.setUserAgentValue(BuildConfig.APPLICATION_ID);
provider.setOsmdroidBasePath(getStorage());
provider.setOsmdroidTileCache(getStorage());

Upvotes: 2

spy
spy

Reputation: 3258

This is actually a strange situation. osmdroid on first start up detects the largest writable storage location for the map tile cache. If none are found, it should default to application provide storage. I think this didn't happen in your case because you didn't call this

Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context));

It probably needs more visibility in the documentation, but this will kick start the discovery process. This should be done before the map view is created.

Upvotes: 1

Make sure you are enabling the runtime permissions correctly.. You will need external storage as well as the internet ones. Also check your manifest permissions

Upvotes: 6

Related Questions