manpagez: man pages & more
html files: geoclue
Home | html | info | man

Master provider: simple example in C

Here is the "Hello World" for Geoclue Master. It shows one of the advantages of using the Master provider: Even most web service providers can be used as signal emitting providers (master queries them whenever network connection changes).

#include <geoclue/geoclue-master.h>

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;
	GeoclueMaster *master;
	GeoclueMasterClient *client;
	GeocluePosition *pos;
	GeocluePositionFields fields;
	double lat, lon;
	GError *error = NULL;
	
	g_type_init ();
	
	/ * create a MasterClient using Master * /
	master = geoclue_master_get_default ();
	client = geoclue_master_create_client (master, NULL, &error);
	g_object_unref (master);
	
	if (!client) {
		g_printerr ("Error creating GeoclueMasterClient: %s\n", error->message);
		g_error_free (error);
		return 1;
	}
	
	/ * Set our requirements: We want at least city level accuracy, require signals, 
	    and allow the use of network (but not e.g. GPS) * /
	if (!geoclue_master_client_set_requirements (client,
	                                             GEOCLUE_ACCURACY_LEVEL_LOCALITY,
	                                             0, TRUE,
	                                             GEOCLUE_RESOURCE_NETWORK,
	                                             &error)){
		g_printerr ("set_requirements failed: %s", error->message);
		g_error_free (error);
		g_object_unref (client);
		return 1;
		
	}
	
	/ * Get a Position object * /
	pos = geoclue_master_client_create_position (client, NULL);
	if (!pos) {
		g_printerr ("Failed to get a position object");
		g_object_unref (client);
		return 1;
	}
	
	/ * call get_position. We do not know which provider actually provides 
	    the answer (although we could find out using MasterClient API) * /
	fields = geoclue_position_get_position (pos, NULL,
	                                        &lat, &lon, NULL,
	                                        NULL, &error);
	if (error) {
		g_printerr ("Error in geoclue_position_get_position: %s.\n", error->message);
		g_error_free (error);
		error = NULL;
	} else {
		if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE &&
		    fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
			g_print ("We're at %.3f, %.3f.\n", lat, lon);
		}
	}
	
	
	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);
	g_object_unref (client);
	return 0;
}
		
© manpagez.com 2000-2017
Individual documents may contain additional copyright information.