I'm making a GPS tracking application with osmdroid map. I have a service which tracks gps data and stores the it in a sqlite databse. The service communicates with the map with a broadcast (every time the location is changed it casts a broadcast message). When the Map gets the broadcast message it gets the latest latitude and longitude from the database and moves the map to the geopoint. So far it works fluently.
Now, I want to overlay the map with the current track, showing the route on it. I tired it with ItemizedIconOverlay this way:
public static void locateMap(Context context) {
try{
SQLiteDatabase database = context.openOrCreateDatabase(DBNAME, MODE_PRIVATE, null);
Cursor cLat = database.rawQuery(latitudeQuery, null);
if (cLat.moveToFirst()) {
latitude = cLat.getDouble(0);
}
else {
latitude = 0;
}
Cursor cLong = database.rawQuery(longitudeQuery, null);
if (cLong.moveToFirst()) {
longitude = cLong.getDouble(0);
}
else {
longitude = 0;
}
database.close();
GeoPoint geoPoint = new GeoPoint(latitude,longitude);
mMapController.animateTo(geoPoint);
mMapController.setCenter(geoPoint);
overlayItems.add(new OverlayItem("","",geoPoint));
Drawable mDrawable = context.getResources().getDrawable(R.drawable.pont);
mResourceProxy = new DefaultResourceProxyImpl(context.getApplicationContext());
mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(overlayItems, mDrawable, null, mResourceProxy);
mMapView.getOverlays().add(mMyLocationOverlay);
}
catch(Exception ex){
String err = (ex.getMessage()==null)?"drawOnMapFailed":ex.getMessage();
Log.v("MAP",err);
}
}
And it works until a click on the map. I get IndexOutOfBoundsException excepion, so i figured the map doesn't really like it this way. Is there something wrong with the code, or is there any other way to overlay osmdroid? Maybe it's something with the way i'm making this application?
UPDATE: i tried with PathOverlay this way, didn't show anything on the map:
PathOverlay mMyLocationOverlay = new PathOverlay(Color.RED, context);
mMyLocationOverlay.addPoint(geoPoint);
mMapView.getOverlays().add(mMyLocationOverlay);
If you want to just display the route you should look at PathOverlay which is designed for that job. Your current approach will end up with an large number of overlays very quickly even if you sort out the bug. With a PathOverlay you will have just the one overlay that you will need to add your new points to. The implementation is optimised to cope with lots of points on and off the screen.