简体   繁体   English

OSMDroid - 地图不显示

[英]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).我正在使用 osmdroid 5.6.5(通过 Maven)。

I define my user agent according to the osmdroid doc.我根据 osmdroid 文档定义了我的用户代理。

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

I have these API settings:我有这些 API 设置:

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?我可以告诉 OSMDroid 使用 sdcard 文件夹以外的东西吗?

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);
        }
    }

Make sure you are enabling the runtime permissions correctly.. You will need external storage as well as the internet ones. 确保正确启用了运行时权限。您将需要外部存储以及Internet存储。 Also check your manifest permissions 同时检查您的清单权限

add below line before this line setContentView(R.layout.activity_osm); 在此行之前添加以下行setContentView(R.layout.activity_osm);

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

This is actually a strange situation. 这实际上是一个奇怪的情况。 osmdroid on first start up detects the largest writable storage location for the map tile cache. 第一次启动时osmdroid会检测到地图图块缓存的最大可写存储位置。 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. 这应该在创建地图视图之前完成。

I have implemented below OpenStreetMap in kotlin platform and it works. 我已经在kotlin平台下实现了OpenStreetMap ,它可以工作。

Add below line build.gradle(app level) 在build.gradle(应用程序级别)行下方添加

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

Create LocationFragment.kt and paste the below code. 创建LocationFragment.kt并粘贴以下代码。

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

The accepted answer by @shane pointed me in the correct direction. @shane 接受的答案为我指明了正确的方向。 But, permissions requests changed:但是,权限请求发生了变化:

If migrating from pre-29 versions, you must also include legacy permissions to external storage <application android:requestLegacyExternalStorage="true" ...如果从 29 之前的版本迁移,您还必须包含对外部存储的旧权限<application android:requestLegacyExternalStorage="true" ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM