android - 无法在 Android 手机上显示 OpenStreetMap

标签 android openstreetmap osmdroid

我想显示 OSM 但我不知道我的代码有什么问题,当我运行它时, map 没有显示:

没有出现错误,但 map 不仅显示,而且显示灰色形式,我使用 eclipse 和 android 手机 Google API 19!

这是我的 MainActivity 代码:

public class MainActivity extends Activity{
private MapView mMapView;
private MapController mController;
private DefaultResourceProxyImpl mResourceProxy;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMapView = (MapView) findViewById(R.id.myOSMmapview);  
    mController = mMapView.getController();  
    //ResourceProxy init  
    mResourceProxy = new DefaultResourceProxyImpl(this);  
    mMapView.setTileSource(TileSourceFactory.MAPNIK);  
    mMapView.setBuiltInZoomControls(true);  
    mMapView.setMultiTouchControls(true);  
    ////Beijing
    GeoPoint center = new GeoPoint(39.901873, 116.326655);  
    mController.setCenter(center);  
    mController.setZoom(14);  
}}

这是我的布局代码:

<?xml version="1.0" encoding="UTF-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent" >  

<org.osmdroid.views.MapView  
    android:id="@+id/myOSMmapview"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tilesource="MapquestOSM"  
    android:clickable="true"  
    android:enabled="true" >  
</org.osmdroid.views.MapView>  

这是我的 AndroidManifest 代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.osmdemo1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<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" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.osmdemo1.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

最佳答案

参见 How to use the osmdroid library来自 osmdroid wiki .

对于 tilesource MAPNIK,您必须在 setTileSource() 之前设置一个有效的用户代理:

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

否则您的应用程序会在 OSM tile 服务器上被阻止。查看tile usage policy获取更多信息。

关于android - 无法在 Android 手机上显示 OpenStreetMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41457336/

相关文章:

php - 检查用户名是否存在于Android代码中

python - 在 python 中使用 lxml iterparse 解析大型 .bz2 文件 (40 GB)。未压缩文件不会出现的错误

android - 启用指南针 OSMDROID

android - 区域圆叠加

android - 如何在android中使用openstreetmap按经纬度获取城市名称

javascript - 弹出键盘时向上滚动文本字段

android - 使用 android 模拟器发送和接收文本

android - osmdroid 的用户代理

openstreetmap - 查询立交桥 API 查询有过桥/铁路的道路

android - 如何使用 Android 支持库中的 Chips 组件?