Embedding map view in an Android application

 

           Map view can easily be embedded directly in to the Android application using few lines of code.
MapView class can tie into current location and can access the sensors. It will show current location, compass showing the current direction in which the user is heading.
Lets create a sample application to demonstrate this.
Application name: MapView
Build Target: Google APIs platform 4.2

Note: We are using Google APIs build target instead of Android 4.2. The reason is Google APIs are not part of standard android library.

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

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

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mapview.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>

<uses-library android:name="com.google.android.maps" />
</application>

</manifest>

Note: Do not forget to add ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION,INTERNET permissions in your manifest file.
And also we need to add <uses-library> tag in the <application> element.
If you leave out the <uses-library> tag, then it will throw ClassNotFoundException at runtime.

Layout xml file:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<com.google.android.maps.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="MapAPIKey"
android:clickable="true" />

</LinearLayout>

Note: We have used fully qualified name of MapView class in the layout file. The reason is this is not part of standard android library.

Activity class:
MainActivity.java

package com.example.mapview;

import android.os.Bundle;
import android.view.Menu;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class MainActivity extends MapActivity {
private MapView map;
private MapController controller;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

private void initMapView() {
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(true);
map.setBuiltInZoomControls(true);
}

/** Start tracking the position on the map. */
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
// overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(8);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
}

Activity class should extend MapActivity. In the initMapView() method we are finding the MapView element of the layout.
getController() method is used to get MapController that can be used to position, zoom the map.
setSatellite() is used to switch to satellite view.
setBuiltInZoomControls() turns on the standard zoom controls.
In initMyLocation() method we are tracking the current location. MyLocationOverlay is provided by Android.
enableMyLocation() method is used to tell the overlay to listen to location updates.
enableCompass() is used to tell the overlay to listen to compass updates.
runOnFirstFix() method is used to tell the overlay what to do the first time it gets a position from location provider.

Source code of this application:
MapView.zip

 

No comments:
Write comments

Popular Posts