为什么OSM中的地图不能在android studio中显示?

时间:2017-04-02 15:08:47

标签: android android-studio openstreetmap osmdroid

我已经在libs目录中添加了这个库 osmdroid jar文件。 slf4j jar文件。

我按照https://androidcookbook.com/Recipe.seam?recipeId=2521中的教程进行了操作 但地图没有显示,我不知道发生了什么 enter image description here

这是我的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')
}

2 个答案:

答案 0 :(得分:0)

看起来您没有设置您的用户代理。尝试通过以下方式进行设置:

Configuration.getInstance().setUserAgentValue(...)

e.g:

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

答案 1 :(得分:0)

OSM需要访问手机的外部存储。

首先,在AndroidManifest中添加以下权限:

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

第二,如果您的SDK版本晚于23,则用户必须手动允许该权限。然后在您的活动中添加以下代码。最好在OSM活动之前启动此活动。否则,在第一次启动该应用程序时,您仍然看不到该地图-您只能在重新启动该应用程序时才能看到该地图。

注意:应该在“ 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);

第一次启动应用程序时,请参阅需要您允许的框图片:

Permission box