Commit cf9eb5e1 authored by Jussi Kukkonen's avatar Jussi Kukkonen

gtk-ui: improve geocoder example

accept free form string to geocode
parent 2f54d5fa
......@@ -33,29 +33,22 @@ 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();
if (argc < 2 || argc > 3) {
g_printerr ("Usage:\n geocode-example <provider_name>\n");
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]);
/* Address we'd like geocoded */
address = geoclue_address_details_new();
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"));
/* Create new GeoclueGeocode */
geocoder = geoclue_geocode_new (service, path);
g_free (service);
......@@ -64,22 +57,41 @@ int main (int argc, char** argv)
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 (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_hash_table_destroy (address);
g_object_unref (geocoder);
return 1;
}
/* Print out coordinates if they are valid */
/* Print out coordinates if they are valid */
if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE &&
fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
......@@ -93,7 +105,6 @@ int main (int argc, char** argv)
g_print ("Latitude and longitude not available.\n");
}
g_hash_table_destroy (address);
geoclue_accuracy_free (accuracy);
g_object_unref (geocoder);
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment