Tutorials for ItemizedOverlay, map dragging, map controls?

Tutorials that use the MapActivity. Many using GPS functionality.

Tutorials for ItemizedOverlay, map dragging, map controls?

Postby mehta_bhavesh » Wed Dec 31, 2008 8:27 am

I am developing an Map app for storing waypoints/tracks. Can someone post a tutorial or code snippets for any of the
the following:

1) Adding an overlay of map controls (transparent image buttons) for zooming in and out as well as dragging the ma p
through gestures and updating the map view

2) Map a bunch of GeoPoints using ItemizedOverlay<OverlayItem> with markers showing the name of the points and order in which they were traveled. Possibly info window on touching the points showing certain information, e.g. description, time, altitude etc.

3) Drawing paths between geo points in a certain order

4) Drawing compass arrow from current location to a touched waypoint.

Thanks in advance.
Posts: 2
Joined: Wed Dec 31, 2008 8:07 am


Postby mehta_bhavesh » Thu Jan 01, 2009 12:56 am

Here's some code that I got working for #2 and some code for adding zoom controls provided by Android.
Don't forget to make your MapView clickable. That was the hardest part to figure out to make the MapView work.

In your MapActivity class

public void onCreate(Bundle savedInstanceState) {
// Make myMapView the explicit view of this app
mMapView = new MapView(this, "YourApiKeyHere");

// add zoom controls
int y = 10;
int x = 10;
MapView.LayoutParams lp;
lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
x, y,
View zoomControls = mMapView.getZoomControls();
mMapView.addView(zoomControls, lp);

// waypoints overlay

// Enable Sattelite-Mode, so we will see the
// Statue of liberty instantly on the screen


Drawable marker = getResources().getDrawable(R.drawable.star_big_on);

marker.setBounds(0, 0, marker.getIntrinsicWidth(),
mWaypointsOverlay = new WaypointsOverlay(this, marker);

List<Overlay> overlayList = mMapView.getOverlays();

OverlayItem oi = mWaypointsOverlay.getItem(0);

//Get the controller, that is used for translation and zooming
MapController mc = mMapView.getController();
// Translate to the Statue of Liberty
//Zoom Very close



The WaypointsOverlay class is:

package com.google.maps.navigator;

import java.util.ArrayList;

import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class WaypointsOverlay extends ItemizedOverlay<OverlayItem> {

private MapActivity mMapActivity;
private LocationDbAdapter mDbHelper;
private Drawable mMarker;
private ArrayList<OverlayItem> locationList;

public WaypointsOverlay(MapActivity mapActivity, Drawable defaultMarker) {
mMarker = defaultMarker;
mMapActivity = mapActivity;
mDbHelper = new LocationDbAdapter(mMapActivity);
locationList = new ArrayList<OverlayItem>();

// Create each of the overlay items included in this layer.

public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);

protected boolean onTap(int i) {
Toast.makeText(mMapActivity, locationList.get(i).getSnippet(), Toast.LENGTH_SHORT).show();


protected OverlayItem createItem(int index) {
OverlayItem oi = locationList.get(index);
return oi;

public int size() {
//Return the number of markers in the collection
int size = locationList.size();
return size;

private void refreshWaypoints() {
Cursor cursor = mDbHelper.fetchAllLocations();
if (cursor.moveToFirst()) {
do {
String title = cursor.getString(cursor
Double latitude = cursor.getDouble(cursor
.getColumnIndex(LocationDbAdapter.KEY_LAT))* 1E6;
Double longitude = cursor.getDouble(cursor
.getColumnIndex(LocationDbAdapter.KEY_LONG))* 1E6;
GeoPoint geopoint = new GeoPoint(latitude.intValue(), longitude.intValue());
OverlayItem oi = new OverlayItem(
} while (cursor.moveToNext());


The mDbHelper instance is used to fetch a cursor that returns all the waypoints stored in SQLite3 database.

Hope this is useful to more people. Still looking for answers to #1, #3, and #4.
Posts: 2
Joined: Wed Dec 31, 2008 8:07 am

Postby pippo » Sat Mar 28, 2009 11:48 am

hi mehta_bhavesh,

I tried to load some points from a sqlite database but it doesn't work. I get an error message: The application ... has stopped unexpectedly. Please try again. I read that i have to take a look on the log but since I am pretty new to android and java I don't understand that log...so maybe could you (or someone else) please held me to find the error(s)?

thx a lot

Code: Select all
I/SystemServer(   50): Starting Notification Manager.
I/SystemServer(   50): Starting Mount Service.
D/MountListener(   50): handleEvent ums_disconnected
I/SystemServer(   50): Starting DeviceMemoryMonitor service
D/MountListener(   50): handleEvent ums_disabled
I/SystemServer(   50): Starting Location Manager.
D/LocationManagerService(   50): Constructed LocationManager Service
I/System.out(   50): #### #### Setting locale to en_US
D/libhardware(   50): using QEMU GPS Hardware emulation
E/GpsLocationProvider(   50): Could not open GPS configuration file /etc/gps.con
E/GpsLocationProvider(   50): java.io.FileNotFoundException: /etc/gps.conf
E/GpsLocationProvider(   50):   at org.apache.harmony.luni.platform.OSFileSystem
E/GpsLocationProvider(   50):   at java.io.FileInputStream.<init>(FileInputStrea
E/GpsLocationProvider(   50):   at com.android.internal.location.GpsLocationProv
E/GpsLocationProvider(   50):   at com.android.server.LocationManagerService._lo
E/GpsLocationProvider(   50):   at com.android.server.LocationManagerService.loa
E/GpsLocationProvider(   50):   at com.android.server.LocationManagerService.loa
E/GpsLocationProvider(   50):   at com.android.server.LocationManagerService.<in
E/GpsLocationProvider(   50):   at com.android.server.ServerThread.run(SystemSer
E/AndroidRuntime(   50): Crash logging skipped, no checkin service
D/GpsLocationProvider(   50): enable
D/GpsLocationProvider(   50): GpsEventThread starting
D/GpsLocationProvider(   50): NetworkThread starting
D/GpsLocationProvider(   50): NetworkThread wait for network
I/SystemServer(   50): Starting Search Service.
I/SystemServer(   50): Starting Clipboard Service.
I/SystemServer(   50): Starting Checkin Service
D/dalvikvm(   50): GC freed 4344 objects / 262912 bytes in 62ms
I/RecoverySystem(   50): No recovery log file
I/RecoverySystem(   50): No recovery intent file
W/CheckinService(   50): No saved kernel log file
I/SystemServer(   50): Starting Wallpaper Service
D/WallpaperService(   50): WallpaperService startup
I/SystemServer(   50): Starting Audio Service
I/SystemServer(   50): Starting HeadsetObserver
W/HeadsetObserver(   50): This kernel does not have wired headset support
I/WindowManager(   50): Menu key state: 0
D/PowerManagerService(   50): system ready!
D/ActivityManager(   50): Start running!
I/Zygote  (   50): Process: zygote socket opened
I/ActivityManager(   50): Start proc com.android.phone for added application com
.android.phone: pid=86 uid=1001 gids={3001, 3003}
I/ActivityManager(   50): Starting activity: Intent { action=android.intent.acti
on.MAIN categories={android.intent.category.HOME} flags=0x10000000 comp={com.and
roid.launcher/com.android.launcher.Launcher} }
I/jdwp    (   86): received file descriptor 10 from ADB
I/ActivityManager(   50): Start proc android.process.acore for activity com.andr
oid.launcher/.Launcher: pid=92 uid=10005 gids={3003}
I/jdwp    (   92): received file descriptor 10 from ADB
I/ActivityThread(   92): Publishing provider com.android.googlesearch.Suggestion
Provider: com.android.googlesearch.SuggestionProvider
I/ActivityThread(   86): Publishing provider mms-sms: com.android.providers.tele
I/ActivityThread(   92): Publishing provider im: com.android.providers.im.ImProv
I/ActivityThread(   86): Publishing provider mms: com.android.providers.telephon
I/ActivityThread(   86): Publishing provider telephony: com.android.providers.te
I/ActivityThread(   86): Publishing provider sms: com.android.providers.telephon
I/ActivityThread(   86): Publishing provider sim: com.android.phone.SimProvider
I/ActivityThread(   92): Publishing provider contacts;call_log: com.android.prov
D/AndroidRuntime(  101):
D/AndroidRuntime(  101): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime(  101): CheckJNI is ON
D/AndroidRuntime(  101): --- registering native functions ---
I/jdwp    (  101): received file descriptor 20 from ADB
D/dalvikvm(   50): GC freed 5504 objects / 262832 bytes in 247ms
D/PhoneApp(   86): [PhoneIntfMgr] publish: com.android.phone.PhoneInterfaceManag
D/PhoneApp(   86): Resetting audio state/mode: IDLE
D/KeyguardViewMediator(   50): pokeWakelock(5000)
D/PackageParser(   50): Scanning package: /data/app/vmdl48793.tmp
D/dalvikvm(   92): GC freed 2342 objects / 147024 bytes in 285ms
D/dalvikvm(   92): GC freed 660 objects / 79688 bytes in 151ms
W/PackageManager(   50): Attempt to re-install geoalp.location without first uni
I/installd(   27): unlink /data/dalvik-cache/data@app@vmdl48793.tmp@classes.dex
D/AndroidRuntime(  101): Shutting down VM
D/dalvikvm(  101): DestroyJavaVM waiting for non-daemon threads to exit
I/dalvikvm(  101): DestroyJavaVM shutting VM down
D/dalvikvm(  101): HeapWorker thread shutting down
D/dalvikvm(  101): HeapWorker thread has shut down
D/jdwp    (  101): JDWP shutting down net...
D/jdwp    (  101): +++ peer disconnected
I/dalvikvm(  101): Debugger has detached; object registry had 1 entries
D/dalvikvm(  101): VM cleaning up
D/dalvikvm(  101): LinearAlloc 0x0 used 529188 of 4194304 (12%)
D/dalvikvm(   50): GC freed 6348 objects / 345384 bytes in 344ms
D/AndroidRuntime(  123):
D/AndroidRuntime(  123): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime(  123): CheckJNI is ON
I/ActivityManager(   50): Displayed activity com.android.launcher/.Launcher: 394
8 ms
D/AndroidRuntime(  123): --- registering native functions ---
I/ActivityManager(   50): Start proc com.google.process.gapps for on-hold: pid=1
29 uid=10016 gids={3003}
I/SurfaceFlinger(   50): Boot is finished (7408 ms)
I/WindowManager(   50): Input configuration changed: { scale=1.0 imsi=0/0 locale
=en_US touch=3 key=2/2 nav=3 orien=1 }
D/dalvikvm(   50): GC freed 2188 objects / 104664 bytes in 367ms
W/GoogleLoginService(  129): Device has no accounts: sending Intent { action=and
D/PackageParser(   50): Scanning package: /data/app/vmdl48794.tmp
I/ActivityManager(   50): Stopping service: com.google.android.googleapps/.Googl
D/PackageManager(   50): Removing package geoalp.location
D/PackageManager(   50):   Activities: geoalp.location.Map
D/PackageManager(   50): Scanning package geoalp.location
I/PackageManager(   50): /data/app/vmdl48794.tmp changed; unpacking
D/installd(   27): DexInv: --- BEGIN '/data/app/vmdl48794.tmp' ---
D/dalvikvm(  143): DexOpt: load 16ms, verify 5ms, opt 0ms
D/installd(   27): DexInv: --- END '/data/app/vmdl48794.tmp' (success) ---
D/PackageManager(   50):   Activities: geoalp.location.Map
I/installd(   27): move /data/dalvik-cache/data@app@vmdl48794.tmp@classes.dex ->
D/PackageManager(   50): New package installed in /data/app/geoalp.location.apk
D/ActivityManager(   50): Uninstalling process geoalp.location
D/AndroidRuntime(  123): Shutting down VM
D/dalvikvm(  123): DestroyJavaVM waiting for non-daemon threads to exit
I/dalvikvm(  123): DestroyJavaVM shutting VM down
D/dalvikvm(  123): HeapWorker thread shutting down
D/dalvikvm(  123): HeapWorker thread has shut down
D/jdwp    (  123): JDWP shutting down net...
D/dalvikvm(  123): VM cleaning up
D/dalvikvm(  123): LinearAlloc 0x0 used 527764 of 4194304 (12%)
D/dalvikvm(   86): GC freed 2654 objects / 164632 bytes in 297ms
D/AndroidRuntime(  149):
D/AndroidRuntime(  149): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime(  149): CheckJNI is ON
D/dalvikvm(   50): GC freed 4710 objects / 223240 bytes in 280ms
D/AndroidRuntime(  149): --- registering native functions ---
D/SystemClock(   86): Setting time of day to sec=1238236517
W/SystemClock(   86): Unable to set rtc to 1238236517: Invalid argument
I/ActivityManager(   50): Starting activity: Intent { flags=0x10000000 comp={geo
alp.location/geoalp.location.Map} }
D/PhoneApp(   86): [CallNotifier] onMwiChanged(): false
D/PhoneApp(   86): [CallNotifier] onCfiChanged(): false
I/ActivityManager(   50): Start proc geoalp.location for activity geoalp.locatio
n/.Map: pid=157 uid=10024 gids={3003}
D/AndroidRuntime(  149): Shutting down VM
D/dalvikvm(  149): DestroyJavaVM waiting for non-daemon threads to exit
I/dalvikvm(  149): DestroyJavaVM shutting VM down
D/dalvikvm(  149): HeapWorker thread shutting down
D/dalvikvm(  149): HeapWorker thread has shut down
D/jdwp    (  149): JDWP shutting down net...
D/dalvikvm(  149): VM cleaning up
D/dalvikvm(  149): LinearAlloc 0x0 used 539556 of 4194304 (12%)
I/jdwp    (  129): received file descriptor 26 from ADB
I/jdwp    (  157): received file descriptor 10 from ADB
I/ActivityManager(   50): Start proc com.android.mms for broadcast com.android.m
ms/.transaction.SmsReceiver: pid=164 uid=10011 gids={3003}
I/System.out(  157): #### #### Setting locale to en_US
I/jdwp    (  164): received file descriptor 10 from ADB
I/ActivityManager(   50): processNextBroadcast: waiting for ProcessRecord{4350ba
00 164:com.android.mms/10011}
W/Maps_Persistence(  157): Couldn't find file:  /data/data/geoalp.location/files
W/Maps    (  157): Couldn't restore map info, data == null
D/dalvikvm(   50): GC freed 3379 objects / 244280 bytes in 108ms
I/ActivityManager(   50): processNextBroadcast: waiting for ProcessRecord{4350ba
00 164:com.android.mms/10011}
D/StatusBar(   50): updateResources
D/AndroidRuntime(  157): Shutting down VM
W/dalvikvm(  157): threadid=3: thread exiting with uncaught exception (group=0x4
E/AndroidRuntime(  157): Uncaught handler: thread main exiting due to uncaught e
E/AndroidRuntime(  157): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{geoalp.location/geoalp.location.Map}: java.lang.NullPointerException

E/AndroidRuntime(  157):        at android.app.ActivityThread.performLaunchActiv
E/AndroidRuntime(  157):        at android.app.ActivityThread.handleLaunchActivi
E/AndroidRuntime(  157):        at android.app.ActivityThread.access$1800(Activi
E/AndroidRuntime(  157):        at android.app.ActivityThread$H.handleMessage(Ac
E/AndroidRuntime(  157):        at android.os.Handler.dispatchMessage(Handler.ja
E/AndroidRuntime(  157):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  157):        at android.app.ActivityThread.main(ActivityThrea
E/AndroidRuntime(  157):        at java.lang.reflect.Method.invokeNative(Native
E/AndroidRuntime(  157):        at java.lang.reflect.Method.invoke(Method.java:5
E/AndroidRuntime(  157):        at com.android.internal.os.ZygoteInit$MethodAndA
E/AndroidRuntime(  157):        at com.android.internal.os.ZygoteInit.main(Zygot
E/AndroidRuntime(  157):        at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  157): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  157):        at geoalp.location.WaypointsOverlay.refreshWaypo
E/AndroidRuntime(  157):        at geoalp.location.WaypointsOverlay.<init>(Waypo
E/AndroidRuntime(  157):        at geoalp.location.Map.onCreate(Map.java:43)
E/AndroidRuntime(  157):        at android.app.Instrumentation.callActivityOnCre
E/AndroidRuntime(  157):        at android.app.ActivityThread.performLaunchActiv
E/AndroidRuntime(  157):        ... 11 more
I/Process (   50): Sending signal. PID: 157 SIG: 3
D/PhoneApp(   86): [CallNotifier] onMwiChanged(): false
I/dalvikvm(  157): threadid=7: reacting to signal 3
D/PhoneApp(   86): [CallNotifier] onCfiChanged(): false
I/dalvikvm(  157): Wrote stack trace to '/data/anr/traces.txt'
D/TelephonyProvider(   86): Setting numeric '310260' to be the current operator
I/ActivityManager(   50): Start proc com.android.alarmclock for broadcast com.an
droid.alarmclock/.AlarmInitReceiver: pid=176 uid=10001 gids={}
I/ActivityManager(   50): Stopping service: com.android.mms/.transaction.SmsRece
I/jdwp    (  176): received file descriptor 10 from ADB
I/ActivityManager(   50): processNextBroadcast: waiting for ProcessRecord{434dd7
28 176:com.android.alarmclock/10001}
I/ARMAssembler(   50): generated scanline__00000077:03515104_00000000_00000000 [
27 ipp] (41 ins) at [0x2567a8:0x25684c] in 1370573 ns
I/ActivityThread(  176): Publishing provider com.android.alarmclock: com.android
D/MobileDataStateTracker(   50): CONNECTED event did not supply interface name.
D/MobileDataStateTracker(   50): DNS server addresses are not known.
E/NetworkStateTracker(   50): net.tcp.buffersize.unknown not found in system pro
perties. Using defaults
E/NetworkStateTracker(   50): Can't set tcp buffer sizes:java.io.FileNotFoundExc
eption: /sys/kernel/ipv4/tcp_rmem_min
D/GpsLocationProvider(   50): updateNetworkState available
D/GpsLocationProvider(   50): NetworkThread out of wake loop
D/GpsLocationProvider(   50): Requesting time from NTP server pool.ntp.org
E/NetworkStateTracker(   50): net.tcp.buffersize.unknown not found in system pro
perties. Using defaults
E/NetworkStateTracker(   50): Can't set tcp buffer sizes:java.io.FileNotFoundExc
eption: /sys/kernel/ipv4/tcp_rmem_min
D/GpsLocationProvider(   50): updateNetworkState available
I/ActivityManager(   50): Start proc com.google.android.apps.maps for broadcast
com.google.android.apps.maps/com.google.android.maps.ff.FriendFinderReceiver: pi
d=184 uid=10000 gids={3003}
D/SntpClient(   50): round trip: 58 ms
D/SntpClient(   50): clock offset: 11176 ms
D/GpsLocationProvider(   50): calling native_inject_time: 1238236530849 referenc
e: 22116 certainty: 29
D/GpsLocationProvider(   50): NetworkThread wait for 14400000ms
I/jdwp    (  184): received file descriptor 10 from ADB
I/ActivityThread(  184): Publishing provider com.google.android.maps.SuggestionP
rovider: com.google.android.maps.provider.MapsSuggestionProvider
I/ActivityThread(  184): Publishing provider com.google.android.maps.FriendsProv
ider: com.google.android.maps.ff.FriendsProvider
I/ActivityThread(  184): Publishing provider com.google.android.maps.SearchHisto
ryProvider: com.google.android.maps.provider.SearchHistoryProvider
D/Maps.FriendFinder(  184): Starting FriendView service
I/ActivityManager(   50): Start proc android.process.media for broadcast com.and
roid.providers.downloads/.DownloadReceiver: pid=191 uid=10004 gids={1006, 3003,
I/jdwp    (  191): received file descriptor 10 from ADB
I/ActivityThread(  191): Publishing provider downloads: com.android.providers.do
I/ActivityThread(  191): Publishing provider drm: com.android.providers.drm.DrmP
D/KeyguardViewMediator(   50): pokeWakelock(5000)
I/ActivityThread(  191): Publishing provider media: com.android.providers.media.
V/MediaProvider(  191): Attached volume: internal
D/dalvikvm(   50): GREF has increased to 201
D/MediaScannerService(  191): start scanning volume internal
I/ActivityManager(   50): Stopping service: com.android.providers.downloads/.Dow
I/ActivityManager(   50): Stopping service: com.android.mms/.transaction.Transac
D/dalvikvm(   50): GC freed 4532 objects / 190728 bytes in 81ms
D/MediaScanner(  191):  prescan time: 387ms
D/MediaScanner(  191):     scan time: 79ms
D/MediaScanner(  191): postscan time: 1ms
D/MediaScanner(  191):    total time: 467ms
D/MediaScannerService(  191): done scanning volume internal
I/ActivityManager(   50): Stopping service: com.android.providers.media/.MediaSc
I/ActivityManager(   50): Stopping service: com.android.providers.downloads/.Dow
I/ActivityManager(   50): Stopping service: com.android.mms/.transaction.SmsRece
W/ActivityManager(   50): Launch timeout has expired, giving up wake lock!
W/ActivityManager(   50): Activity idle timeout for HistoryRecord{43469d88 {geoa
D/dalvikvm(   92): GC freed 4096 objects / 207440 bytes in 48ms
D/dalvikvm(   86): GC freed 3980 objects / 208128 bytes in 60ms
Posts: 3
Joined: Wed Mar 04, 2009 9:51 am


Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests