geocode-example.c 3.17 KB
Newer Older
Jussi Kukkonen's avatar
Jussi Kukkonen committed
1 2 3 4
/*
 * Geoclue
 * geocode-example.c - Example using the Geocode client API
 *
Jussi Kukkonen's avatar
Jussi Kukkonen committed
5 6 7
 * Provider options are not used in this sample. See other files for
 * examples on that.
 * 
Jussi Kukkonen's avatar
Jussi Kukkonen committed
8
 * Author: Jussi Kukkonen <jku@openedhand.com>
Jussi Kukkonen's avatar
Jussi Kukkonen committed
9
 * Copyright 2007, 2008 by Garmin Ltd. or its subsidiaries
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
Jussi Kukkonen's avatar
Jussi Kukkonen committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 */

#include <glib.h>
#include <geoclue/geoclue-geocode.h>

int main (int argc, char** argv)
{
	gchar *service, *path;
	GeoclueGeocode *geocoder = NULL;
	GeocluePositionFields fields;
	GHashTable *address = NULL;
	double lat, lon;
	GeoclueAccuracy *accuracy = NULL;
	GError *error = NULL;
	
	g_type_init();
	
Jussi Kukkonen's avatar
Jussi Kukkonen committed
43 44
	if (argc < 2 || argc > 3) {
		g_printerr ("Usage:\n  geocode-example <provider_name>\n");
Jussi Kukkonen's avatar
Jussi Kukkonen committed
45 46
		return 1;
	}
Iain Holmes's avatar
Iain Holmes committed
47
	g_print ("Using provider '%s'\n", argv[1]);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
48 49 50
	service = g_strdup_printf ("org.freedesktop.Geoclue.Providers.%s", argv[1]);
	path = g_strdup_printf ("/org/freedesktop/Geoclue/Providers/%s", argv[1]);
	
Jussi Kukkonen's avatar
Jussi Kukkonen committed
51
	/* Address we'd like geocoded */
52
	address = geoclue_address_details_new();
Jussi Kukkonen's avatar
Jussi Kukkonen committed
53 54 55 56
	g_hash_table_insert (address, g_strdup ("postalcode"), g_strdup ("00330"));
	g_hash_table_insert (address, g_strdup ("countrycode"), g_strdup ("FI"));
	g_hash_table_insert (address, g_strdup ("locality"), g_strdup ("Helsinki"));
	g_hash_table_insert (address, g_strdup ("street"), g_strdup ("Solnantie 24"));
Jussi Kukkonen's avatar
Jussi Kukkonen committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	
	
	/* Create new GeoclueGeocode */
	geocoder = geoclue_geocode_new (service, path);
	g_free (service);
	g_free (path);
	if (geocoder == NULL) {
		g_printerr ("Error while creating GeoclueGeocode object.\n");
		return 1;
	}
	
	
	/* Geocode. We're not interested in altitude 
	   this time, so leave it NULL. */
	fields = geoclue_geocode_address_to_position (geocoder, address, 
	                                              &lat, &lon, NULL, 
	                                              &accuracy, &error);
	if (error) {
		g_printerr ("Error while geocoding: %s\n", error->message);
		g_error_free (error);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
77 78 79
		g_hash_table_destroy (address);
		g_object_unref (geocoder);
		
Jussi Kukkonen's avatar
Jussi Kukkonen committed
80 81 82 83 84 85 86 87
		return 1;
	}
	/* Print out coordinates if they are valid */
	if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE &&
	    fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
		
		GeoclueAccuracyLevel level;
		
Jussi Kukkonen's avatar
Jussi Kukkonen committed
88 89
		geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
		g_print ("Geocoded position (accuracy level %d): \n", level);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
90 91 92 93 94 95
		g_print ("\t%f, %f\n", lat, lon);
		
	} else {
		g_print ("Latitude and longitude not available.\n");
	}
	
Jussi Kukkonen's avatar
Jussi Kukkonen committed
96
	g_hash_table_destroy (address);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
97
	geoclue_accuracy_free (accuracy);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
98
	g_object_unref (geocoder);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
99 100 101
	return 0;
	
}