Getting periodic location updates using GPS

This snippet will enable you to get the periodic updates of a device location using GPS satellites. PRIVILEGE NEEDED: http://tizen.org/privilege/location

//    PRIVILEGE needed to be set in tizen-manifest.xml:
//    http://tizen.org/privilege/location


#include <locations.h>
#include <dlog.h> // for logging purposes

// Declare location manager (in this example it is a global variable):
location_manager_h location_manager;

// Defining callback function for location update
// The callback will be called periodically, as defined during callback registration.
void position_updated_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
{
    LOGI("Location update: Latitude: %f, Longitude: %f, timestamp: %d", latitude, longitude, timestamp);
}

// On startup of your service:

void startup()
{
    // Creating location manager for Global Positioning System
    // The LOCATIONS_METHOD_GPS setting means that this location manager will use
    // only GPS satellites to check the device's location.
    if(location_manager_create(LOCATIONS_METHOD_GPS, &location_manager) == LOCATIONS_ERROR_NONE)
    {
        LOGI("Location manager created.");
        // Setting callback for position update - update will be done every 15 seconds.
        if (location_manager_set_position_updated_cb(location_manager, position_updated_cb, 15, NULL) == LOCATIONS_ERROR_NONE)
        {
            LOGI("Location position update callback added.");
        }
        // Starting service
        if(location_manager_start(location_manager)== LOCATIONS_ERROR_NONE)
        {
            LOGI("Location service started.");
        }
    }
}

// On shutdown of your service:

void shutdown()
{
    //unset the callback
    if (location_manager_unset_position_updated_cb(location_manager) == LOCATIONS_ERROR_NONE)
    {
        LOGI("Callback unregistered.");
    }
    // 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;
}

Responses

0 Replies