无法在Android手机上显示OpenStreetMap

时间:2017-01-04 06:16:34

标签: android openstreetmap osmdroid

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

没有出现错误,但地图不显示,并显示灰色表格,我使用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>

1 个答案:

答案 0 :(得分:0)

请参阅How to use the osmdroid library中的osmdroid wiki

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

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

否则您的应用程序将在OSM磁贴服务器上被阻止。有关详细信息,请参阅tile usage policy