How to Use Tizen Native Geocode API in 3 Steps
PUBLISHED
Since Tizen 2.4
What is Geocoding?
Geocoding translates the place address into its geographical coordinates.
The Geocode API is one of the Maps Services provided by the Tizen Native Location Framework.
To start using the Geocode API:
- Create an empty Tizen native application.
- Start the Maps Service.
- Run a geocode request.
Prerequisites
This document assumes that you already have basic knowledge in Tizen development. For basic information, see https://developer.tizen.org/development/getting-started/preface.
The Maps Service API requires a security key issued by the maps provider.
In case of HERE maps, the security key is a concatenation of app_id
and app_code
generated on https://developer.here.com/plans/api/consumer-mapping according to your consumer plan.
“your-security-key” is “app_id/app_code”
To ensure the Maps Services API execution, set the following privileges:
http://tizen.org/privilege/mapservice
http://tizen.org/privilege/internet
http://tizen.org/privilege/network.get
1 Create an Empty Tizen Native Application
In the IDE, create an empty application using the basic UI application template, and run it on an emulator or a device.
The "Hello Tizen" label appears on the screen at application startup.
2 Start the Maps Service
To start the Maps Service:
- Include the Maps Service API header file:
#include <maps_service.h>
- Add a Maps Service handle to the
appdata_s
structure:
typedef struct appdata { Evas_Object *win; Evas_Object *conform; Evas_Object *label; maps_service_h maps; // Maps Service handle } appdata_s;
- Create the Maps Service Instance in the
app_create()
function:
static bool app_create(void *data) { appdata_s *ad = data; create_base_gui(ad); // Specify the maps provider name if(maps_service_create("HERE", &ad->maps) != MAPS_ERROR_NONE) return false; // Set the security key issued by the maps provider maps_service_set_provider_key(ad->maps, "your-security-key"); return true; }
- When no longer needed, destroy the Maps Service instance in the
app_terminate()
function:
static void app_terminate(void *data) { // Release all resources appdata_s *ad = data; maps_service_destroy(ad->maps); }
3 Run a Geocode Request
To run a geocode request:
- Add the geocode request into the
app_create()
function:
// Use the Geocode API int req_id = 0; maps_service_geocode(ad->maps, "Moscow Bolshoi Theatre", NULL, geocode_cb, ad, &req_id);
- Implement the geocode callback:
static bool geocode_cb(maps_error_e result, int request_id, int index, int total, maps_coordinates_h coordinates, void *user_data) { // Get latitude and longitude of the geocode double latitude = .0; double longitude = .0; maps_coordinates_get_latitude(coordinates, &latitude); maps_coordinates_get_longitude(coordinates, &longitude); // Output the geocode char geocode[64] = {0}; snprintf(geocode, 64, "Geocode is: (%f, %f)", latitude, longitude); appdata_s *ad = user_data; elm_object_text_set(ad->label, geocode); // Release the coordinates handle maps_coordinates_destroy(coordinates); // If return true, you receive other geocodes // of the address put to the Geocode request // In this example, 1 geocode is enough return false; }
- Run the application.
At first, the familiar "Hello Tizen" line appears. A moment later, however, it changes to "Geocode is: (55.756970, 37.615020)”.
This indicates the geocode of the Bolshoi Theatre in Moscow.
Reference
https://developer.tizen.org/development/tutorials/native-application/location/maps-service#geocode