the Flagship of independent news, reviews and resources for Developers and Users of Samsung's mobile platform Bada
Bada Tutorials, Communication & Location

Bada Example: Showing your location on the map

wit, December 16th, 2009

We have talkedĀ  yesterday with Adrian about showing the user’s location on a map. Actually this one is pretty simple to achieve, there is only one little thing that might cause some trouble (I needed some time to figure it out!). We will take the bada project we have created yesterday and build upon it.

The idea is very simple: we need to register a new listener that would periodically receive the user’s location and center our map on it. The first thing to keep in mind is that you will need the event injector for this one, since the simulator has no other way to determine its location. But let us start with something else…

First of all, you will need to add another privilege to the manifest.

<Privileges>
 <Privilege><Name>MAP_SERVICE</Name></Privilege>
 <Privilege><Name>LOCATION</Name></Privilege>
 </Privileges>

Then you will need, once again, to extend your app’s “inheritance” :-) by a few additional functionalities. Go to the header and add the following code:

class SimpleLocation :
 public Application, IActionEventListener, ILocationListener
{

// Lot's of declarations go here
// [...]

 /**
 * Called when an action is performed
 */
 void OnActionPerformed(const Control& source, int actionId);

 /**
 * Called when a location event is received
 */
 void OnLocationUpdated(Location& location);

 /**
 * Called when the status of the provider has changed
 */
 void OnProviderStateChanged(LocProviderState newState);

};

#endif

The second function is irrelevant for this demo – what we really need is OnLocationUpdated(). Before your application can receive the location updates, you will need to register it as a listener to do so. Somewhere at the beginning of OnAppInitializing() function you will need to add some code like:

 LocationProvider *locProvider = new LocationProvider();
 locProvider->Construct(LOC_METHOD_GPS);
 locProvider->RequestLocationUpdates(*this, 5, false);

OurĀ  location provider delivers location updates, which it gets through the GPS method (the only method available right now, others, like wifi hotspots and what not, are to follow later). We register our app as listener and ask for an update each 5 seconds. This means that each 5 seconds OnLocationUpdated() function, which we will have to define right now, is going to be called.

void
SimpleLocation::OnLocationUpdated(Location& location)
{
    if(location.GetQualifiedCoordinates()!=null)
    {
        const QualifiedCoordinates *q = location.GetQualifiedCoordinates();
        double longitude = q->GetLongitude();
        double latitude = q->GetLatitude();

        pMap->SetCenter(latitude, longitude, true);
        RedrawMap();
    }
}

void
SimpleLocation::OnProviderStateChanged(LocProviderState newState){}

Now this one has been somewhat tricky to get it to run properly. In my case, the whole simulator crashed each time I tried to read the qualified coordinates. The source was quiet simple: although the function gets called each 5 seconds, the location passed to it does not always contain any coordinates. Sometimes it just gives a null pointer. This has been solved simply by testing if the pointer to the qualified coordinates is null, and only proceed if it is not.

Build the project and run it. When you see your app, right click on the simulator window and select the events injector in the context menu. There select the Location tab. In the lower half you will see a map, you can click on it to set a location and press on the button below to send it to the device. Your app will get notified within 5 seconds. Nicely done!

bada_sdk_event_injector_location

Now this is simply a demo and probably not a very useful app. As you may notice the app will center your position each 5 seconds making any navigation on the map somewhat annoying. Your task is to think something up to avoid that – if the user needs to take a look at the “surroundings” he shouldn’t be forced to his location each 5 seconds :-) ! Have fun!

Related posts:

  1. Bada how-to: map marker overlay
  2. bada Tutorial: zooming a map with softkeys
  3. Displaying a Map in a bada application
  1. 5 Responses to “Bada Example: Showing your location on the map”

  2. By Mike on Dec 16, 2009 | Reply

    Thanks.Great start.

  3. By milbeo on May 12, 2010 | Reply

    By the way, thanks for this very useful tuto!
    I’d like to add a zoom effect animation to this. I found things like:
    pMap->ZoomAnimationEffect( true );
    pMap->ZoomEffect( zoom );

    but it doesn’t work and I didn’t find any example…
    Could someone please help me??

    Thanks all!

  4. By James on Aug 30, 2010 | Reply

    Great Tut! Could You post the entire code for this or put it in the http://badadevprojects.svn.sourceforge.net/ SVN repo please?!?!

  1. 2 Trackback(s)

  2. Dec 18, 2009: bada Dev » Blog Archive » Bada tutorial: simple screen orientation example
  3. Jan 25, 2010: bada Dev » Blog Archive » Bada how-to: map marker overlay

Post a Comment

Editor's picks

Copyright 2009-2010 BadaDev.com (unless otherwise stated). All rights reserved! Powered by Wordpress!