using-geoclue.xml 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?xml version="1.0"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">

<chapter>
	<title>Using Geoclue in applications</title>
	<section id="overview">
		<title>Overview</title>
		<sect2>
			<subtitle>Why use Geoclue</subtitle>
			<para>
				Building context-sensitive applications is difficult. Location-sensitivy is no 
				exception, but Geoclue tries to help there. With Geoclue it is possible to build
				location-aware applications with less work and less knowledge about the actual 
				geoinformation sources.
			</para>
			<para> Main features of Geoclue are:
				<itemizedlist>
				<listitem>Standardized D-Bus (and GObject) interfaces to various geographical data sources</listitem>
				<listitem>Easy and fast to implement: Need to get data from provider XYZ? 
				Chances are, Geoclue is the easiest way to do that...</listitem>
				<listitem>Geoclue Master provider (experimental at the moment) makes things eaven easier: 
				you don't even have to choose a data provider, just let Geoclue choose the
				best possible provider for you.</listitem>
Jussi Kukkonen's avatar
Jussi Kukkonen committed
24 25 26
				<listitem>Adding new providers (data sources) is straight-forward: Even when the geoinformation
				source you want is not already in Geoclue, Implementing a Geoclue provider may
				well turn out to be the easiest way forward.</listitem>
27 28 29 30 31 32
				</itemizedlist>
			</para>
		</sect2>
		<sect2>
			<subtitle>Requirements</subtitle>
			<para>
Jussi Kukkonen's avatar
Jussi Kukkonen committed
33
				Geoclue is a collection of D-Bus APIs. Internally it's implemented with GLib and GObject (as is the C wrapper library). 
34 35 36 37 38 39 40 41
				Several included providers also use Libxml2, and the master provider uses GConf. 
			</para>
			<para>
				There is a GObject-based C API, but using Geoclue is possible from any programming language 
				with D-Bus support. 
			</para>
		</sect2>
		<sect2>
Jussi Kukkonen's avatar
Jussi Kukkonen committed
42
			<subtitle>Using basic Geoclue providers</subtitle>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
			<para>
				D-Bus can automatically start services as they're needed so basic method calls
				can be made without any preparation. However, geoclue providers may shut down when 
				they're not used. This means that clients using signals and clients that set any options
				should always use AddReference() and RemoveReference() methods to ensure server
				persistence. In C the wrapper takes care of this.
			</para>
			<para>
				Clients should not assume that method calls will return instantly or that the 
				response will contain valid data. Examples of possible problems
				<itemizedlist>
					<listitem>Many geoclue providers use web services so method calls may take seconds or even time out.</listitem>
					<listitem>GPS providers usually respond instantly, but will not have a valid data until after getting a fix (which may take anything from a few seconds to minutes)</listitem>
				</itemizedlist>
				Using signals should be the first option whenever the used provider supports them.
				When signals are not available, asynchronous method calls can be used in 
				applications where UI responsiveness is important.
			</para>
		</sect2>
		<sect2>
			<subtitle>Using Geoclue Master</subtitle>
			<para>Master provider was designed to solve the following problems:
				<itemizedlist>
					<listitem>A single provider cannot be the best solution to all problems</listitem>
					<listitem>The "best" providers will be different depending on the user</listitem>
					<listitem>Supporting a single geodata source can be a big job for an application developer, supporting several would be a nightmare</listitem>
				</itemizedlist>
			</para>
			<para>
				Master provider implements the same interfaces as regular providers, but internally 
				it uses whichever regular provider happens to be the best one (with "best" defined as 
				a provider that fulfills the accuracy requirements and does not surpass resource restrictions).
				The internally used provider may change over time -- this does not interrupt clients use in any way,
				but the changes can be observed using the MasterClient API.
			</para>
			<para>
				A typical Master provider use includes:
				<itemizedlist>
				<listitem>Getting a client-specific GeoclueMasterClient from GeoclueMaster</listitem>
				<listitem>Setting GeoclueMasterClient requirements (such as accuracy)</listitem>
				<listitem>Starting the wanted interfaces (such as Position) </listitem>
				<listitem>Using the client just like a regular provider</listitem>
				</itemizedlist>
			</para>
			
			<para>
				Master provider is fairly new and may not be as stable as the rest of Geoclue.
			</para>
		</sect2>
Jussi Kukkonen's avatar
Jussi Kukkonen committed
92 93 94 95
		<sect2>
			<subtitle>Current providers</subtitle>
			TODO: list the providers shipped with geoclue, shortly explain their features.
		</sect2>
96 97
	</section>
	<section id="simple-example">
Jussi Kukkonen's avatar
Jussi Kukkonen committed
98
		<title>Using basic Geoclue providers: simple example in C</title>
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		<para>There are several examples in the geoclue source, these are included here to 
		give a quick overview.
		</para>
		<para>
			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).
		</para>
		<programlisting>
#include &lt;geoclue/geoclue-position.h&gt;

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,
	                                        &amp;lat, &amp;lon, NULL,
	                                        NULL, &amp;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 &amp; GEOCLUE_POSITION_FIELDS_LATITUDE &amp;&amp;
	    fields &amp; 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;
}
		</programlisting>
		<para>Save as test-hostip.c and compile with 
		</para>
		<programlisting>
gcc `pkg-config --libs --cflags geoclue`  -o test-hostip test-hostip.c
		</programlisting>
		<para>Here is a similarly simple example using GLib mainloop and Gypsy provider with 
		position-changed signals: 
		</para>
		<programlisting>
#include &lt;geoclue/geoclue-position.h&gt;

/ * 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 &amp; GEOCLUE_POSITION_FIELDS_LATITUDE &amp;&amp;
	    fields &amp; 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, &amp;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;
}
		</programlisting>
	</section>
	<section id="simple-master-example">
		<title>Master provider: simple example in C</title>
		<para>
			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).
		</para>

		<programlisting>
#include &lt;geoclue/geoclue-master.h&gt;

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 &amp; GEOCLUE_POSITION_FIELDS_LATITUDE &amp;&amp;
	    fields &amp; 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, &amp;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,
	                                             &amp;error)){
		g_printerr ("set_requirements failed: %s", error->message);
		g_error_free (error);
		g_object_unref (client);
		return 1;
		
	}
	
	/ * Get a Position object * /
289
	pos = geoclue_master_client_create_position (client, NULL);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	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,
	                                        &amp;lat, &amp;lon, NULL,
	                                        NULL, &amp;error);
	if (error) {
		g_printerr ("Error in geoclue_position_get_position: %s.\n", error->message);
		g_error_free (error);
		error = NULL;
	} else {
		if (fields &amp; GEOCLUE_POSITION_FIELDS_LATITUDE &amp;&amp;
		    fields &amp; 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;
}
		</programlisting>
	</section>
</chapter>