为什么OSM中的Map不能在Android Studio中显示?

64jmpszr  于 2023-05-18  发布在  Android
关注(0)|答案(3)|浏览(239)

我已经在libs目录osmdroid jar文件中添加了这个库。slf4j jar文件。
我按照教程在https://androidcookbook.com/Recipe.seam?recipeId=2521,但Map没有显示,我不知道发生了什么

这是我的MainAcitvity.java

package com.example.pc_parinussa.osm;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ScaleBarOverlay;

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.mapview);
        mMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
        mMapView.setBuiltInZoomControls(true);
        mMapController = (MapController) mMapView.getController();
        mMapController.setZoom(13);
        GeoPoint gPt = new GeoPoint(51500000, -150000);
        mMapController.setCenter(gPt);
    }
}

我的activity_main.xml

<?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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mapview">
    </org.osmdroid.views.MapView>

</LinearLayout>

我的摇篮

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.pc_parinussa.osm"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile files('libs/osmdroid-android-4.0.jar')
}
tsm1rwdh

tsm1rwdh1#

将此行添加到onCreate()

getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this))
eimct9ow

eimct9ow2#

看来您没有设置用户代理。尝试通过以下方式设置:
Configuration.getInstance().setUserAgentValue(...)
例如:
Configuration.getInstance().setUserAgentValue(getPackageName())

oxcyiej7

oxcyiej73#

OSM需要访问您的电话的外部存储。
首先,在AndroidManifest中添加以下权限:

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

其次,如果您的SDK版本超过23,用户必须手动允许权限。然后在你的活动中添加以下代码。最好是在与OSM的活动之前启动该活动;否则,在第一次启动应用程序时,您仍然无法看到Map-您只能在重新启动应用程序时看到Map。

注意:“setContentView(R.layout.your_layout_name)”之前应该写代码;“

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
             && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
      ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
  } 

 setContentView(R.layout.your_layout_name);

请参见第一次启动App时需要您许可的框图片:
Permission box

相关问题