Geoclue Reference Manual |
---|
There are several examples in the geoclue source, these are included here to give a quick overview.
Here is a very simple example of using a specific Geoclue Position provider. Note that we're using the synchronous version of the method call here, so geoclue_position_get_position() will block until the response comes (or until D-Bus timeouts).
#include <geoclue/geoclue-position.h> int main() { GeocluePosition *pos; GeocluePositionFields fields; double lat, lon; GError *error = NULL; g_type_init (); / * Create the position object * / pos = geoclue_position_new ("org.freedesktop.Geoclue.Providers.Hostip", "/org/freedesktop/Geoclue/Providers/Hostip"); / * call get_position. Note that unneeded output variables (here timestamp, altitude and accuracy) can be left NULL * / fields = geoclue_position_get_position (pos, NULL, &lat, &lon, NULL, NULL, &error); if (error) { g_printerr ("Error in get_position: %s.\n", error->message); g_error_free (error); g_object_unref (pos); return 1; } if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE && fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) { g_print ("According to Hostip.info we're at %.3f, %.3f.\n", lat, lon); } else { g_print ("Hostip does not have a valid location available.\nVisit http://www.hostip.info/ to correct this"); } g_object_unref (pos); return 0; }
Save as test-hostip.c and compile with
gcc `pkg-config --libs --cflags geoclue` -o test-hostip test-hostip.c
Here is a similarly simple example using GLib mainloop and Gypsy provider with position-changed signals:
#include <geoclue/geoclue-position.h> / * device name or bluetooth address * / #define GPS_DEVICE_NAME "00:02:76:C5:81:BF" static void position_changed (GeocluePosition *position, GeocluePositionFields fields, int timestamp, double latitude, double longitude, double altitude, GeoclueAccuracy *accuracy, gpointer userdata) { g_print ("Position changed:\n"); if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE && fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) { g_print ("\t%f, %f\n\n", latitude, longitude); } else { g_print ("\tLatitude and longitude not available.\n\n"); } } int main() { GMainLoop *loop; GHashTable *options; GeocluePosition *pos; GError *error = NULL; g_type_init (); / * Create the position object * / pos = geoclue_position_new ("org.freedesktop.Geoclue.Providers.Gypsy", "/org/freedesktop/Geoclue/Providers/Gypsy"); / * Set GPS device name option for Gypsy * / options = g_hash_table_new (g_str_hash, g_str_equal); g_hash_table_insert (options, "org.freedesktop.Geoclue.GPSDevice", GPS_DEVICE_NAME); if (!geoclue_provider_set_options (GEOCLUE_PROVIDER (pos), options, &error)) { g_printerr ("Error setting options: %s\n", error->message); g_error_free (error); g_hash_table_destroy (options); g_object_unref (pos); return 1; } g_hash_table_destroy (options); / * connect to signal * / g_signal_connect (G_OBJECT (pos), "position-changed", G_CALLBACK (position_changed), NULL); g_print ("Waiting for position change signals...\n"); loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (loop); g_main_loop_unref (loop); g_object_unref (pos); return 0; }