This code places an Elementary map widget on a window, to exemplify part of the widget's API, related to overlays.
We'll start this example in the same way as Map Example 1. Adding a map with buttons to control zoom, so if you didn't read it yet, just do it now.
elm_object_text_set(bt, "+");
elm_box_pack_end(box, bt);
elm_object_text_set(bt, "-");
elm_box_pack_end(box, bt);
elm_object_text_set(bt, "X");
elm_box_pack_end(box, bt);
elm_object_text_set(bt, "#");
elm_box_pack_end(box, bt);
Overlays can be placed over the map to represent anything we want. Let's say we want to represent some countries and cities with overlays.
Before we create city or country overlays, let's create class overlays.
city_clas = elm_map_overlay_class_add(map);
These lines create a class overlay which represents cities. This class overlay will be used for grouping city overlays. Later city overlays in the same class are appended to this class overlay. if city overlays are near each other, they will be grouped.
We can set the icon for the class so that the icon will be displayed when city overlays are grouped. We can set the zoom required to display the overlays that belongs to this class, so if the zoom is less than this value, nothing will be shown.
Country class can be created in the same way.
country_clas = elm_map_overlay_class_add(map);
Next we'll create some overlays representing cities and countries. We set the data for the overlay so that can be used later when clicked callback is called. We'll append them into city class to be grouped. We'll append them in a list, to close up them later. To create a default overlay, we need to pass the coordinates.
ovl = elm_map_overlay_add(map, -43.2, -22.9);
We subscribe a smart callback "overlay,clicked" to create bubble on the clicked overlay.
Finally, on our main
function, we ask the map to show all the overlays with the biggest zoom possible, passing the list of overlays added.
We have created a specific structure for this example to store the name of the place and a path to a image file to represent it.
typedef struct _Overlay_Data
{
const char *name;
const char *file;
} Overlay_Data;
We'll create instances for each place:
Overlay_Data data_argentina = {"Argentina", NULL};
Overlay_Data data_chile = {"Chile", NULL};
Overlay_Data data_sampa = {"São Paulo", NULL};
Overlay_Data data_rio = {"Rio de Janeiro", NULL};
Overlay_Data data_brasilia = {"Brasília", NULL};
const char *data_dir;
{
return icon;
}
{
char buf[256];
snprintf(buf, sizeof(buf), "%s/images/icon_07.png", data_dir);
return _icon_get(obj, buf);
}
{
char buf[256];
snprintf(buf, sizeof(buf), "%s/images/icon_05.png", data_dir);
return _icon_get(obj, buf);
}
{
char buf[256];
snprintf(buf, sizeof(buf), "%s/images/icon_06.png", data_dir);
return _icon_get(obj, buf);
}
{
char buf[256];
snprintf(buf, sizeof(buf), "%s/images/icon_04.png", data_dir);
return _icon_get(obj, buf);
}
{
evas_object_image_file_set(img, data->file, NULL);
evas_object_image_filled_set(img,
EINA_TRUE);
evas_object_size_hint_min_set(img, 64, 64);
elm_box_pack_end(bx, img);
elm_object_text_set(label, data->name);
elm_box_pack_end(bx, label);
return bx;
}
static void
{
printf("Overlay clicked\n");
if (!bubble) bubble = elm_map_overlay_bubble_add(map);
}
static void
{
int zoom;
}
static void
{
int zoom;
}
static void
{
}
static void
{
}
_nasty_hack(void *data)
{
Evas *e = evas_object_evas_get(o);
}
EAPI_MAIN int
{
char buf[255];
snprintf(buf, sizeof(buf), "%s/images/rock_01.jpg", "sdf");
data_argentina.file = strdup(buf);
snprintf(buf, sizeof(buf), "%s/images/rock_02.jpg", "sdf");
data_chile.file = strdup(buf);
snprintf(buf, sizeof(buf), "%s/images/sky_01.jpg", "sdf");
data_sampa.file = strdup(buf);
snprintf(buf, sizeof(buf), "%s/images/sky_02.jpg", "sdf");
data_rio.file = strdup(buf);
snprintf(buf, sizeof(buf), "%s/images/sky_03.jpg", "sdf");
To return an icon, all we need to do is to add a elm_icon and return it:
For the content, let's return something more elaborate. We will return a box with an image representing the place, and the name of this place:
{
evas_object_image_file_set(img, data->file, NULL);
evas_object_image_filled_set(img,
EINA_TRUE);
evas_object_size_hint_min_set(img, 64, 64);
elm_box_pack_end(bx, img);
elm_object_text_set(label, data->name);
elm_box_pack_end(bx, label);
return bx;
}
See map_example_02.c for full source, whose window should look like this picture: