Tizen Native API  5.5
Map Example - Creation and Zoom

This code places an Elementary map widget on a window, to exemplify part of the widget's API.

Let's start adding a map to our window:

   map = elm_map_add(win);
   evas_object_size_hint_weight_set(map, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   elm_win_resize_object_add(win, map);
   evas_object_show(map);

It's enough to display a world map inside our window. But usually you'll need to let user interact with the map. We need to place some buttons, so the user could control the map. It's done on the following code. If you don't know about boxes, or buttons, check their examples, Box Example 1 and Button Example 1.

   box = elm_box_add(win);
   evas_object_show(box);

   bt = elm_button_add(win);
   elm_object_text_set(bt, "+");
   elm_box_pack_end(box, bt);
   evas_object_show(bt);
   evas_object_smart_callback_add(bt, "clicked", _bt_zoom_in, map);

   bt = elm_button_add(win);
   elm_object_text_set(bt, "-");
   elm_box_pack_end(box, bt);
   evas_object_show(bt);
   evas_object_smart_callback_add(bt, "clicked", _bt_zoom_out, map);

   bt = elm_button_add(win);
   elm_object_text_set(bt, "X");
   elm_box_pack_end(box, bt);
   evas_object_show(bt);
   evas_object_smart_callback_add(bt, "clicked", _bt_zoom_fit, map);

   bt = elm_button_add(win);
   elm_object_text_set(bt, "#");
   elm_box_pack_end(box, bt);
   evas_object_show(bt);
   evas_object_smart_callback_add(bt, "clicked", _bt_zoom_fill, map);

We are adding callback functions that will be called when the user clicks over these buttons. Let's study such functions, starting from the function that will zoom in the map:

static void
_bt_zoom_in(void *data, Evas_Object *obj EINA_UNUSED, void *ev EINA_UNUSED)
{
   int zoom;
   elm_map_zoom_mode_set(data, ELM_MAP_ZOOM_MODE_MANUAL);
   zoom = elm_map_zoom_get(data);
   elm_map_zoom_set(data, zoom + 1);
}

First thing done is assure zoom mode is set to manual. It's the default mode, but the other buttons will change this, so before setting a new zoom value, we need to change the zoom mode.

Then, we get the current zoom value, increment that, and set the new value to the map. If it's bigger than max zoom value allowed, it will remain on the maximum allowed, nothing bad will happen. This way we don't need to check first if it won't be bigger than max.

Zoom out function is basically the same thing, but zoom will be decremented instead of incremented:

static void
_bt_zoom_out(void *data, Evas_Object *obj EINA_UNUSED, void *ev EINA_UNUSED)
{
   int zoom;
   elm_map_zoom_mode_set(data, ELM_MAP_ZOOM_MODE_MANUAL);
   zoom = elm_map_zoom_get(data);
   elm_map_zoom_set(data, zoom - 1);
}

The "X" button, when pressed, will call a function that will zoom the map until it fits inside the scroll frame with no pixels outside this area:

static void
_bt_zoom_fit(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   elm_map_zoom_mode_set(data, ELM_MAP_ZOOM_MODE_AUTO_FIT);
}

And the "#" button, will call a function that will zoom until map fills scroll, ensuring no pixels are left unfilled:

static void
_bt_zoom_fill(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   elm_map_zoom_mode_set(data, ELM_MAP_ZOOM_MODE_AUTO_FILL);
}

But we can also set map to show something different from default world map, changing the zoom level and region shown. Let's pick a wonderful city coordinates, one placed at 43 20 S, 22 90 W . Since map uses double variables to represent latitude and longitude, to represent north or east, we should represent it as positive values, and south or west as negative. Also, the value will be represented as degree.min. So, for example, our longitude 43 20 S will be represented by the value -43.20 . A zoom set to 12 should be enough to show a city.

   elm_map_region_show(map, -43.2, -22.9);

See map_example_01.c for full source, whose window should look like this picture:

map_example_01.png