summaryrefslogtreecommitdiff
path: root/example/revgeocode-example.c
blob: 2207234683dd0c7a77ce649ff6044fd8b5ee795e (plain)
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
113
114
115
116
117
118
119
120
/*
 * Geoclue
 * revgeocode-example.c - Example using the ReverseGeocode client API
 *
 * Author: Jussi Kukkonen <jku@openedhand.com>
 * Copyright 2007 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 <stdlib.h>
#include <geoclue/geoclue-reverse-geocode.h>

/* GHFunc, use with g_hash_table_foreach */
static void
print_address_key_and_value (char *key, char *value, gpointer user_data)
{
	g_print ("    %s: %s\n", key, value);
}

static GHashTable *
parse_options (int    argc,
               char **argv)
{
        GHashTable *options;
        int i;

        options = g_hash_table_new (g_str_hash, g_str_equal);
        for (i = 4; i < argc; i += 2) {
                g_hash_table_insert (options, argv[i], argv[i + 1]);
        }

        return options;
}

int main (int argc, char** argv)
{
	gchar *service, *path;
	GeoclueReverseGeocode *revgeocoder = NULL;
	GeoclueAccuracy *accuracy, *out_accuracy;
	GHashTable *address = NULL;
	double lat, lon;
	GError *error = NULL;

	g_type_init();

	if (argc < 4) {
		g_printerr ("Usage:\n  revgeocode-example <provider_name> <lat> <lon>\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 GeoclueReverseGeocode */
	revgeocoder = geoclue_reverse_geocode_new (service, path);
	g_free (service);
	g_free (path);
	if (revgeocoder == NULL) {
		g_printerr ("Error while creating GeoclueGeocode object.\n");
		return 1;
	}


        /* Set options */
        if (argc > 4) {
                GHashTable *options;

                options = parse_options (argc, argv);
                if (!geoclue_provider_set_options (GEOCLUE_PROVIDER (revgeocoder), options, &error)) {
                        g_printerr ("Error setting options: %s\n",
                                    error->message);
                        g_error_free (error);
                        error = NULL;
                }
                g_hash_table_destroy (options);
        }

	address = g_hash_table_new_full (g_str_hash, g_str_equal,
	                                 (GDestroyNotify)g_free,
	                                 (GDestroyNotify)g_free);

	lat = atof (argv[2]);
	lon = atof (argv[3]);
	accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_STREET, 0.0, 0.0);
	if (!geoclue_reverse_geocode_position_to_address (revgeocoder,
	                                                  lat, lon, accuracy,
	                                                  &address, &out_accuracy, &error)) {
		g_printerr ("Error while reverse geocoding: %s\n", error->message);
		g_error_free (error);
		g_object_unref (revgeocoder);
		return 1;
	}

	/* Print out the address */
	GeoclueAccuracyLevel level;
	geoclue_accuracy_get_details (out_accuracy, &level, NULL, NULL);
	g_print ("Reverse Geocoded  [%f, %f] to address (accuracy %d):\n", lat, lon, level);
	g_hash_table_foreach (address, (GHFunc)print_address_key_and_value, NULL);

	g_hash_table_destroy (address);
	g_object_unref (revgeocoder);
	return 0;

}