can't display OpenStreetMap on an Android phone

1.4k Views Asked by At

I want to display OSM but i don't know what's the problem with my code, when I run it, the map didn't display:

No error appear but the map didn't only display, and display the grey form, I work with eclipse and android phone Google API 19!

Here is my MainActivity code:

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

Here is my layout code:

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

Here is my AndroidManifest code:

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

There are 1 best solutions below

6
On

See How to use the osmdroid library from the osmdroid wiki.

For the tilesource MAPNIK you have to set a valid user-agent before setTileSource():

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

Otherwise your application gets blocked on the OSM tile servers. See the tile usage policy for more information.