Tuesday, 8 May 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


1 comment: