db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join

MapMe

Introduction

This application is based on BrowseMap by Davanum Srinivas, a simple Google Maps viewer for Android which lets you:

  • Browse Google maps on Android
  • Zoom in and out
  • Move around with the mouse (could be touchscreen on a real device)
  • Toggle traffic and satellite view
  • Find location

(The basic functionality is the same as the Maps application that already ships with Android)

However MapMe adds several features on top of this basic functionality thanks to db4o object persistence engine such as:

  • Bookmark location to db4o (full map persistence including zoom levels and satellite & traffic view)
  • Browse list of bookmarks
  • Edit bookmark
  • Navigate to location on map from bookmark
  • Center map on current GPS reported location

Screenshots

To see the application in action you can watch the MapMe video on YouTube, a high resolution video (30 Mb) on an external server or the following screenshots:

db4o headquarters (satellite + traffic view) 

MapMe main menu:

Creation of bookmark/navpoint:

List of saved bookmarks/navpoints:

How MapMe uses db4o

If you check the source code (see downloads below) you'll see that the DB helper class that handles all persistence operations (db opening, storage, querying, counting) is below 80 lines of code! 

It is all about simplicity without loosing performance. Let's see some of the persistence methods used in the project.

Opening the database

Opening the database is pretty simple and automatically creates the db file if necessary. Once opened db4o locks the db file.

public ObjectContainer db(){

      try {

            if(oc == null || oc.ext().isClosed())

                  oc = Db4o.openFile(dbConfig(), db4oDBFullPath(context));

            return oc;

      } catch (Exception e) {

            Log.e(Db4oHelper.class.getName(), e.toString());

            return null;

      }

}

 

private String db4oDBFullPath(Context ctx) {

      return ctx.getDataDir() + "/" + "browsemap.db4o";

}

Note tha the path were we host the datbase path is relative the context data directory.

Configuring the database

Here we provide a configuration objects which is passed when we open the database (see above).

private Configuration dbConfig(){

      Configuration c = Db4o.newConfiguration();

      c.objectClass(MapBookmark.class).objectField("name").indexed(true);

      c.objectClass(MapBookmark.class).updateDepth(2);

      c.objectClass(MapBookmark.class).minimumActivationDepth(3);

      c.objectClass(MapBookmark.class).cascadeOnDelete(true);

      return c;

} 

Note the established indexing on the field "name" for MapBookmark objects and the activation and update levels.

Saving a bookmark

We call this a set operation where the object is updated or inderted depending on whether it exists on the database.

public void setBookmark(

                        String name,

                        String description,

                        int latitude,

                        int longitude,

                        int zoomLevel,

                        boolean satellite,

                        boolean traffic){

     

      MapBookmark bkm = getBookmark(name);

      if(bkm == null)

            bkm = new MapBookmark(name);

      bkm.setDescription(description);

      bkm.setLatitude(latitude);

      bkm.setLongitude(longitude);

      bkm.setZoomLevel(zoomLevel);

      bkm.setSatellite(satellite);

      bkm.setTraffic(traffic);

      db().set(bkm);

      db().commit();

} 

Here we set all the fields in the object before the set operation.

Querying a bookmark by name

We use Query by Example passing a prototype to retrieve an bookamrk by name.

public MapBookmark getBookmark(String name){

      MapBookmark proto = new MapBookmark(name);

      ObjectSet result = db().get(proto);

      if(result.hasNext()){

            return (MapBookmark)result.next();

      }

      return null;

}

QBE uses reflection to check the fields of the prototype object.

Querying for all bookmarks

Here we copy the SODA query result to an ArrayList. However, note that you don't need to do this when you use Native Queries (which in this case would return a List<MapBookmark> directly):

public List<MapBookmark> getBookmarkList(){

      ArrayList<MapBookmark> ret = new ArrayList<MapBookmark>();

        ObjectSet result = getBookmarks();

        while (result.hasNext())

            ret.add((MapBookmark)result.next());

        return ret;

}

   

private ObjectSet getBookmarks(){

      Query query = db().query();

      query.constrain(MapBookmark.class);

      query.descend("name").orderAscending();

      return query.execute();

} 

Deleting a bookmark

public void deleteBookmark(String name) {

      MapBookmark bkm = getBookmark(name);

      if(bkm != null){

            db().delete(bkm);

            db().commit();

      }