Tuesday, May 8, 2012

Simple example use osmdroid and slf4j-android to implement OpenStreetMapView on Android

In order to implement OpenStreetMapView for Android, we have to Prepare Java Build Path to osmdroid and slf4j-android libs for OpenStreetMapView.

it's a simple example to implement OpenStreetMapView on Android.

OpenStreetMap on Android


Modify main.xml to include org.osmdroid.views.MapView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
 <org.osmdroid.views.MapView
     android:id="@+id/openmapview"
     android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
     
</LinearLayout>


Main activity, AndroidOpenStreetMapViewActivity.java
package com.exercise.OpenStreetMapView;

import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;

import android.app.Activity;
import android.os.Bundle;

public class AndroidOpenStreetMapViewActivity extends Activity {
 
 private MapView myOpenMapView;
 private MapController myMapController;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myOpenMapView = (MapView)findViewById(R.id.openmapview);
        myOpenMapView.setBuiltInZoomControls(true);
        myMapController = myOpenMapView.getController();
        myMapController.setZoom(4);
    }

}


Modify AndroidManifest.xml to add uses-permission of "android.permission.ACCESS_NETWORK_STATE", "android.permission.INTERNET" and "android.permission.WRITE_EXTERNAL_STORAGE".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exercise.OpenStreetMapView"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <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:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidOpenStreetMapViewActivity"
            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>

</manifest>


Download the files.

Next:
- Update location on OpenStreetMap


8 comments:

Oxxi said...

application stoped

Çağatay Odabaşı said...

Thank you for helpful example..

Unknown said...

the application crashes... with a fatal exception error in main..

Anonymous said...

La aplicación abre y todo, pero no muestra el mapa :/

Anonymous said...

Hello... i am not able to view the map data ... only rectangles showing me ... help me to fix this

saturday10am said...

hello i appreciate your post, but i have a problem.
i made android project by using android studio, and then i programmed code that your same code.
problem is "Problem downloading MapTile" "HTTP/1.1 403 forbidden"..
why this refuse for request? and how can i success run this project?
thank you for watching my question.

saturday10am said...

hello poster~
i checked error, second ago.
osm data is free for everyone but, tile server are not, so i think that "Valid User-Agent identifying application" must be sent.

Unknown said...

Thanks a lot