the Flagship blog & community for App Developers with main focus on Samsung's bada and cross-platform technologies
Bada Tutorials, Communication & Location, UI & Graphics

bada Tutorial: zooming a map with softkeys

, December 15th, 2009

We continue with our project from the first map tutorial. What we want to do now is pretty simple: we want to be able to zoom in and out of the map with the help of two softkeys. Softkeys (usually two) are “buttons” that are placed in the lower area (corners) of a form.

Of course, the map control itself already provides some simple navigation functionality: you can drag the map to all sides and you can also zoom in and zoom out (just press long on the map: zooming will appear!). Nevertheless we want to be able to do that more user friendly: through the softkeys :-)

bada_sdk_map_zoom

For that purpose you will need to add those softkeys to the form first. As opposed to the usual buttons, softkeys are part of a form’s properties. You will see the relevant items right away!

bada_ide_form_softkeys

Enable the softkeys and insert some meaningful values as text. Save the form – we are done here. Next switch over to the header of your app’s class. As described in another example, we will need to add some stuff in here so that the application itself can deal with the action events. Basically we need three things:

  1. Define some constant integer values to identify the zooming actions
  2. Inherit the app’s class also from the IActionEventListener
  3. Implement the necessary handler for events OnActionPerformed()
const int BUTTON_ZOOM_IN = 1;
const int BUTTON_ZOOM_OUT = 2;

/**
 * [SimpleLocation] application must inherit from Application class
 * which provides basic features necessary to define an application.
 */
class SimpleLocation :
 public Application, IActionEventListener
{
    private:
        Frame *pFrame;
        Form *pForm;
        Map *pMap;
/**
 * Called when the battery level changes.
 */
 void OnBatteryLevelChanged(BatteryLevel batteryLevel);

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

Next I have refactored the code from the previous example a little and put the initialization code into the corresponding function instead of keeping it in the OnForeground function. The drawing itself has been basically reduced to this:

void
SimpleLocation::OnForeground(void)
{
    RedrawMap();
}

void
SimpleLocation::RedrawMap(void)
{
    pMap->Draw();
    pFrame->Draw();
    pFrame->Show();

    return;
}

The rest has been transfered into the OnAppInitializing() function, where we also need to bind the action IDs for our zooming softkeys, as well as telling bada that the application’s class itself is going to deal with the action events.

bool
SimpleLocation::OnAppInitializing(AppRegistry& appRegistry)
{
    result r;
    String providerName = L"deCarta";
    String extraInfo = L"ClientName=yourlogin;ClientPassword=yourpass;HostUrl=http://ws.decarta.com/openls/openls";
    int width = 0;
    int height = 0;
    Coordinates center = Coordinates();
    center.Set(37.786505, -122.39862, 0);

    // bind the form and set it as current
    pFrame = this->GetAppFrame()->GetFrame();
    pForm = new Form();
    pForm->Construct("IDF_FORM1");
    pFrame->AddControl(*pForm);
    pFrame->SetCurrentForm(*pForm);

    // add action IDs and listeners to the soft keys
    pForm->SetSoftkeyActionId(SOFTKEY_0,BUTTON_ZOOM_IN);
    pForm->SetSoftkeyActionId(SOFTKEY_1,BUTTON_ZOOM_OUT);
    pForm->AddSoftkeyActionListener(SOFTKEY_0, *this);
    pForm->AddSoftkeyActionListener(SOFTKEY_1, *this);

    // Connect to OSP map service provider
    IMapServiceProvider* pProvider =
    static_cast<IMapServiceProvider*>(ProviderManager::ConnectToServiceProviderN
                            (providerName, LOC_SVC_PROVIDER_TYPE_MAP, extraInfo));
    if (null == pProvider) goto CATCH;

    // Create map
    pMap = new Map();
    if (null == pMap)  goto CATCH;

    // Specify map size (the whole space)
    // we take the whole available space on the form
    width=pForm->GetCanvasN()->GetBounds().width;
    height=pForm->GetCanvasN()->GetBounds().height;
    r = pMap->Construct(Osp::Graphics::Rectangle(0, 0, width, height), *pProvider);
    if (IsFailed(r)) goto CATCH;

    // Set desired zoom level
    r = pMap->SetZoomLevel(17, false);
    if (IsFailed(r)) goto CATCH;

    // Set center position to display
    r = pMap->SetCenter(center, false);
    if (IsFailed(r)) goto CATCH;

    // Enable my location
    pMap->SetMyLocationEnabled(true);

    // Render a map
    pForm->AddControl(*pMap);

    return true;

CATCH:
    delete pProvider;
}

Well, finally we only need to deal with the zooming events accordingly! It is all about modifying the map’s zoom level and redrawing it all. Please keep in mind that it is a quick&dirty solution meant to demonstrate not to teach :-) – I do not check whether the zoom level crosses any limits in my code.

void
SimpleLocation::OnActionPerformed(const Control& source, int actionId)
{
    int zoomlevel = pMap->GetZoomLevel();
    switch (actionId)
    {
       case BUTTON_ZOOM_IN:
           zoomlevel++;
           pMap->SetZoomLevel(zoomlevel, true);
           RedrawMap();
           break;

       case BUTTON_ZOOM_OUT:
           zoomlevel--;
           pMap->SetZoomLevel(zoomlevel, true);
           RedrawMap();
           break;

       default:
           break;
    }
}

Build it, run it, enjoy it! :-)

Related posts:

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

  2. By Adrian on Dec 15, 2009 | Reply

    Thanks for the tutorial. You explain better by every tutorial.

    A nice idea would be to center the map around the user’s current location.

  3. By AMora on Dec 15, 2009 | Reply

    “Sorry may english is bad.”
    Hello dear wit.
    I’m very happy because you are very active for work on Bada® OS :-) GoGO Bada, GoGO wit

  4. By wit on Dec 15, 2009 | Reply

    Thank you guys for your support! AMora, no need to apologize – English is not my native language, too :-D

    Actually I am experimenting with location & event injection at this very moment. Will post my findings later :-)

  5. By Adrian on Dec 15, 2009 | Reply

    All right wit! Good luck!

    Oh… one more thing. Why don’t you install/activate Akismet and get rid of the captcha? It’s really annoying.

  6. By wit on Dec 15, 2009 | Reply

    Wow, thanks for the tip with Akismet, Adrian :-)

  7. By Adrian on Dec 15, 2009 | Reply

    You’re welcome. Much better now.

  8. By Rémy DAVID on Apr 8, 2010 | Reply

    You said “you can also zoom in and zoom out (just press long on the map: zooming will appear!)”
    I don’t manage to enable that behaviour, how did you do exactly ? (I got SDK beta2RC2)

  9. By wit on Apr 10, 2010 | Reply

    Hmmm… I don’t have the beta2rc2, but if I remember it right, you just pressed with your finger on the map for a longer period of time and some sort of zooming mechanism magically appeared :-)

  10. By Cristian Baita on Dec 4, 2010 | Reply

    Hi, I have a big problem with this:

    “Coordinates center = Coordinates();
    center.Set(37.786505, -122.39862, 0);”

    gives me the following error:
    Osp::Locations::Coordinates::Set (234) [E_INVALID_ARG] Converted from “IllegalArgumentException”

    So, basically I cannot center the view where I want… I’ve tried everything, even writing (double(23.000093),double(43.333), float(0)), it just doesn’t seem to admit any params as valid :(

  11. By Cristian Baita on Dec 4, 2010 | Reply

    I’ve done some more testing… On the Simulator it does OK, but this problem with invalid coordinates appears only on the device, Samsung Wave. So, the same application compiled and executed on the Simulator works perfect, and compiled and executed on the device gives me this error and doesn’t center on the location I need… What could I do? Why is this happening?

  12. By Thomas on May 4, 2011 | Reply

    Hi everybody,

    Thanks you very much for this tuto…
    Thanks to this topic and the samples MapViewer of the bada SDK. I am pretty closed to the solution for seeing a map in my Samsung Wave 1.
    Unfortunately, I get an error due to my login and password of DeCarta. What do I have to put in the parameters ClientName=yourlogin;ClientPassword=yourpass

    “yourlogin” is my login Decarta, the name I have given to my application in DecartaDeveloper zone, or other things?
    “yourpass” is my API´s client number, my password in Decarta or other things?

    Could you help me please?

    Thomas from France

  13. By slogger on Oct 5, 2011 | Reply

    How make own overlay with zoom control ?

  1. 2 Trackback(s)

  2. Dec 16, 2009: bada Dev » Blog Archive » Bada Example: Showing your location on the map
  3. Jun 13, 2010: BadaDev » Using Hard Keys Event Listeners across Forms

Post a Comment

Editor's picks

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