File: geocode-example.c

package info (click to toggle)
geoclue 0.12.0-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,132 kB
  • sloc: ansic: 12,414; sh: 10,127; xml: 2,036; makefile: 687
file content (112 lines) | stat: -rw-r--r-- 3,515 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Geoclue
 * geocode-example.c - Example using the Geocode client API
 *
 * Provider options are not used in this sample. See other files for
 * examples on that.
 * 
 * Author: Jussi Kukkonen <jku@openedhand.com>
 * Copyright 2007, 2008 by Garmin Ltd. or its subsidiaries
 *
 * 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.
 *
 */

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

int main (int argc, char** argv)
{
	gchar *service, *path;
	GeoclueGeocode *geocoder = NULL;
	GeocluePositionFields fields;
	double lat, lon;
	GeoclueAccuracy *accuracy = NULL;
	GError *error = NULL;
	
	g_type_init();
	
	if (argc < 2) {
		g_printerr ("Usage:\n  geocode-example <provider_name> [\"freeform address\"]\n\n"
		            "  If freeform address is not given, a hard coded address hash "
		            "will be used.\n");
		return 1;
	}

	g_print ("Using provider '%s'\n", argv[1]);
	service = g_strdup_printf ("org.freedesktop.Geoclue.Providers.%s", argv[1]);
	path = g_strdup_printf ("/org/freedesktop/Geoclue/Providers/%s", argv[1]);
	/* 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;
	}

	if (argc == 2) {
		GHashTable *address = NULL;
		/* Address we'd like geocoded */
		address = geoclue_address_details_new();
		geoclue_address_details_insert (address, "locality", "Helsinki");
		geoclue_address_details_insert (address, "postalcode", "00330");
		geoclue_address_details_insert (address, "countrycode", "FI");
		geoclue_address_details_insert (address, "street", "Solnantie 24");

		/* 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);
		g_hash_table_destroy (address);
	} else {
		char *str;
		str = g_strjoinv (" ", &argv[2]);
		fields = geoclue_geocode_freeform_address_to_position
				(geocoder, str,
				 &lat, &lon, NULL,
				 &accuracy, &error);
		g_free (str);
	}

	if (error) {
		g_printerr ("Error while geocoding: %s\n", error->message);
		g_error_free (error);
		g_object_unref (geocoder);
		
		return 1;
	}

	    /* Print out coordinates if they are valid */
	if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE &&
	    fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
		
		GeoclueAccuracyLevel level;
		
		geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
		g_print ("Geocoded position (accuracy level %d): \n", level);
		g_print ("\t%f, %f\n", lat, lon);
		
	} else {
		g_print ("Latitude and longitude not available.\n");
	}
	
	geoclue_accuracy_free (accuracy);
	g_object_unref (geocoder);
	return 0;
	
}