Getting location using GPS
In this example the current location of the device is taken whenever the location service changes its state to enabled. For continuous location update see another snippet.
PRIVILEGE NEEDED: http://tizen.org/privilege/location
#include <locations.h>
#include <dlog.h> // for logging purposes
// Declaring location manager (in this example it is a global variable):
location_manager_h location_manager;
// Defining callback function for location service state change
// The callback will be run each time the location service state changes.
// The state will change for example when the user turns location on/off
// in the phone settings.
void location_state_cb(location_service_state_e state, void *user_data)
{
if (state == LOCATIONS_SERVICE_ENABLED)
{
LOGI("Location service enabled.");
double altitude, latitude, longitude, climb, direction, speed;
double horizontal, vertical;
location_accuracy_level_e level;
time_t timestamp;
int err = location_manager_get_location(location_manager, &altitude, &latitude, &longitude,
&climb, &direction, &speed, &level,
&horizontal, &vertical, ×tamp);
if (err == LOCATIONS_ERROR_NONE)
{
LOGI("Location: %f,%f,%f,%f,%f,%f,%f,%f", altitude, latitude, longitude, climb, direction, speed, level, horizontal, vertical);
}
else
{
LOGE("Location error!");
//handle error here
}
}
if (state == LOCATIONS_SERVICE_DISABLED)
{
LOGI("Location is disabled!");
}
}
// On startup of your service:
// Creating location manager for GPS
// The LOCATIONS_METHOD_GPS setting means that this location manager will use
// only GPS satellites information to check the device's location.
if (location_manager_create(LOCATIONS_METHOD_GPS, &location_manager) == LOCATIONS_ERROR_NONE)
{
// Setting callback for state change
if(location_manager_set_service_state_changed_cb(location_manager, location_state_cb, NULL) == LOCATIONS_ERROR_NONE)
{
LOGI("Location service callback added.");
}
// Starting service
if(location_manager_start(location_manager) == LOCATIONS_ERROR_NONE)
{
LOGI("Location service started.");
}
}
// On shutdown of your service:
if (location_manager_unset_service_state_changed_cb(location_manager) == LOCATIONS_ERROR_NONE)
{
LOGI("Location service callback removed.");
}
// It is not necessary to call location_manager_stop() just before calling
// location_manager_destroy because the service is stopped automatically
// before destruction.
if (location_manager_destroy(location_manager) == LOCATIONS_ERROR_NONE)
{
LOGI("Location manager destroyed.");
location_manager = NULL;
}