int location_geoclue_get_location(location_geoclue_state_t *state, location_t *location) { GeocluePositionFields fields; GError *error = NULL; double latitude = 0, longitude = 0; fields = geoclue_position_get_position(state->position, NULL, &latitude, &longitude, NULL, NULL, &error); if (error) { g_printerr(_("Could not get location: %s.\n"), error->message); g_error_free(error); return -1; } if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE && fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) { fprintf(stdout, _("According to the geoclue provider" " we're at: %.2f, %.2f\n"), latitude, longitude); } else { g_warning(_("Provider does not have a valid location available.")); return -1; } location->lat = latitude; location->lon = longitude; return 0; }
int location_geoclue_get_location(location_geoclue_state_t *state, float *lat, float *lon) { GeocluePositionFields fields; GError *error = NULL; double latitude = 0, longitude = 0; /* Due to timeouts, retry this 3 times. */ int retries = 4; while(--retries) { fields = geoclue_position_get_position(state->position, NULL, &latitude, &longitude, NULL, NULL, &error); if (error) { g_printerr(_("Could not get location: %s.\n"), error->message); g_error_free(error); return -1; } if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE && fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) { break; } g_warning(_("Could not get location, %d retries left.\n"), retries); /* Sleep for a while to let connectivity catch up. */ usleep(1000000); } if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE && fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) { fprintf(stdout, _("According to the geoclue provider" " we're at: %.2f, %.2f\n"), latitude, longitude); } else { g_warning(_("Provider does not have a valid location available.")); return -1; } *lat = latitude; *lon = longitude; return 0; }
void GeolocationServiceGtk::updateLocationInformation() { ASSERT(m_geocluePosition); GOwnPtr<GError> error; GOwnPtr<GeoclueAccuracy> accuracy; GeocluePositionFields fields = geoclue_position_get_position(m_geocluePosition, &m_timestamp, &m_latitude, &m_longitude, &m_altitude, &accuracy.outPtr(), &error.outPtr()); if (error) { setError(PositionError::POSITION_UNAVAILABLE, error->message); return; } else if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE && fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)) { setError(PositionError::POSITION_UNAVAILABLE, "Position could not be determined."); return; } }
int main (int argc, char** argv) { gchar *service, *path; GeocluePosition *pos = NULL; GeocluePositionFields fields; int timestamp; double lat, lon; GeoclueAccuracy *accuracy = NULL; GMainLoop *mainloop; GError *error = NULL; g_type_init(); if (argc < 2 || argc % 2 != 0) { g_printerr ("Usage:\n position-example <provider_name> [option value]\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]); mainloop = g_main_loop_new (NULL, FALSE); /* Create new GeocluePosition */ pos = geoclue_position_new (service, path); if (pos == NULL) { g_printerr ("Error while creating GeocluePosition object.\n"); return 1; } g_free (service); g_free (path); if (argc > 2) { GHashTable *options; options = parse_options (argc, argv); if (!geoclue_provider_set_options (GEOCLUE_PROVIDER (pos), options, &error)) { g_printerr ("Error setting options: %s\n", error->message); g_error_free (error); error = NULL; } g_hash_table_destroy (options); } /* Query current position. We're not interested in altitude this time, so leave it NULL. Same can be done with all other arguments that aren't interesting to the client */ fields = geoclue_position_get_position (pos, ×tamp, &lat, &lon, NULL, &accuracy, &error); if (error) { g_printerr ("Error getting position: %s\n", error->message); g_error_free (error); g_object_unref (pos); return 1; } /* Print out coordinates if they are valid */ if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE && fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) { GeoclueAccuracyLevel level; double horiz_acc; geoclue_accuracy_get_details (accuracy, &level, &horiz_acc, NULL); g_print ("Current position:\n"); g_print ("\t%f, %f\n", lat, lon); g_print ("\tAccuracy level %d (%.0f meters)\n", level, horiz_acc); } else { g_print ("Latitude and longitude not available.\n"); } geoclue_accuracy_free (accuracy); g_signal_connect (G_OBJECT (pos), "position-changed", G_CALLBACK (position_changed_cb), NULL); g_main_loop_run (mainloop); return 0; }