Question
I'm working on a Tizen native application where I need to:
- Display a map
- Allow users to drop pins by clicking on the map
- Retrieve the latitude and longitude coordinates of the clicked location
I've successfully created a basic map using the following code:
ad->map = elm_map_add(ad->conform);
evas_object_size_hint_weight_set(ad->map, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(ad->map, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_map_zoom_mode_set(ad->map, ELM_MAP_ZOOM_MODE_MANUAL);
elm_map_zoom_set(ad->map, 14);
evas_object_show(ad->map);
elm_map_region_show(ad->map, 72.866041, 21.229097);
elm_object_content_set(ad->conform, ad->map);
Elm_Map_Overlay *ovl = elm_map_overlay_add(ad->map, 72.866041, 21.229097);
elm_map_overlay_displayed_zoom_min_set(ovl, 8);
Evas_Object *icon = elm_icon_add(ad->map);
elm_icon_standard_set(icon, "home");
elm_map_overlay_icon_set(ovl, icon);
evas_object_smart_callback_add(ad->map, "clicked", _map_clicked_cb, ad);
Currently, I'm using static coordinates (72.866041, 21.229097). My challenges are:
- How to get the current location first
- How to retrieve the latitude and longitude when a user clicks on the map to drop a pin
I'm using Tizen Studio 2.0 and Mapzen maps (I already have a Mapzen API key).
Answer
Problem Understanding
The user needs to:
- Display a map in a Tizen native application
- Capture user clicks on the map to drop pins
- Retrieve the geographic coordinates (latitude/longitude) of the clicked location
Solution Methods
-
Using Mapzen Maps:
- First, set up Mapzen maps with your API key
- Use the
elm_map_canvas_to_region_convert()function to convert canvas coordinates to geographic coordinates when the user clicks
-
Complete Solution:
- In your click callback function (
_map_clicked_cb), use the following approach:
static void _map_clicked_cb(void *data, Evas_Object *obj, void *event_info) { Evas_Event_Mouse_Down *ev = event_info; double latitude, longitude; // Convert canvas coordinates to geographic coordinates elm_map_canvas_to_region_convert(obj, ev->canvas.x, ev->canvas.y, &latitude, &longitude); // Now you have the latitude and longitude of the clicked location // You can add a pin/marker at this location Elm_Map_Overlay *pin = elm_map_overlay_add(obj, latitude, longitude); // Configure your pin as needed } - In your click callback function (
-
Reverse Geocoding:
- Once you have the coordinates, you can use Mapzen's reverse geocoding API to get address information:
// Use the latitude and longitude to make a reverse geocoding request // to Mapzen's API (https://mapzen.com/documentation/search/reverse/)
Code Examples
Here's a complete example for handling map clicks:
// Add this to your map creation code
evas_object_smart_callback_add(ad->map, "clicked", _map_clicked_cb, ad);
// Click callback implementation
static void
_map_clicked_cb(void *data, Evas_Object *obj, void *event_info)
{
AppData *ad = data;
Evas_Event_Mouse_Down *ev = event_info;
double lat, lon;
// Convert click coordinates to geographic coordinates
elm_map_canvas_to_region_convert(obj, ev->canvas.x, ev->canvas.y, &lat, &lon);
// Add a pin at the clicked location
Elm_Map_Overlay *pin = elm_map_overlay_add(obj, lat, lon);
Evas_Object *icon = elm_icon_add(obj);
elm_icon_standard_set(icon, "location");
elm_map_overlay_icon_set(pin, icon);
// Store or use the coordinates as needed
printf("Clicked at: %f, %f\n", lat, lon);
}
Additional Tips
- Make sure you have proper permissions in your manifest file:
<privileges> <privilege>http://tizen.org/privilege/mapservice</privilege> <privilege>http://tizen.org/privilege/internet</privilege> </privileges> - For current location, consider using the Location Manager API
- Remember that Mapzen services might have usage limits based on your API key