2 #include "maps_test_search_route.h"
3 #include "maps_test_log.h"
4 #include "maps_test_util.h"
7 static maps_distance_unit_e g_distance_unit = MAPS_DISTANCE_UNIT_M;
17 static void cleanup_callback(int error)
25 static bool __maps_route_segment_maneuver_cb(int index, int total, maps_route_maneuver_h maneuver, void *user_data)
27 if (!maneuver) return false;
30 LOG_START("%smaneuver [%d/%d] = {", LOG_PREFIX, index+1, total);
32 if (maps_route_maneuver_get_instruction_text(maneuver, &lv.strval) == MAPS_ERROR_NONE)
33 LOG_PRINT("%s%sinstruction = %s", LOG_PREFIX, LOG_PREFIX, lv.strval);
35 if (maps_route_maneuver_get_distance_to_next_instruction(maneuver, &lv.dblval) == MAPS_ERROR_NONE)
36 LOG_PRINT("%s%sdistance = %.2f %s", LOG_PREFIX, LOG_PREFIX, lv.dblval, get_distance_unit_string(g_distance_unit));
38 if (maps_route_maneuver_get_time_to_next_instruction(maneuver, &lv.intval) == MAPS_ERROR_NONE)
39 LOG_PRINT("%s%stime = %d sec", LOG_PREFIX, LOG_PREFIX, lv.intval);
41 if (maps_route_maneuver_get_direction_id(maneuver, (maps_route_direction_e*)&lv.intval) == MAPS_ERROR_NONE)
42 LOG_PRINT("%s%sdirection_id = %s (%d)", LOG_PREFIX, LOG_PREFIX, get_direction_string(lv.intval), lv.intval);
44 if (maps_route_maneuver_get_turn_type(maneuver, (maps_route_turn_type_e*)&lv.intval) == MAPS_ERROR_NONE)
45 LOG_PRINT("%s%sturn_type = %s (%d)", LOG_PREFIX, LOG_PREFIX, get_turn_type_string(lv.intval), lv.intval);
47 maps_coordinates_h coord;
48 if (maps_route_maneuver_get_position(maneuver, &coord) == MAPS_ERROR_NONE) {
50 maps_coordinates_get_latitude(coord, &lat);
51 maps_coordinates_get_longitude(coord, &lng);
52 LOG_PRINT("%s%sposition = %f, %f", LOG_PREFIX, LOG_PREFIX, lat, lng);
53 maps_coordinates_destroy(coord);
56 if (maps_route_maneuver_get_road_name(maneuver, &lv.strval) == MAPS_ERROR_NONE)
57 LOG_PRINT("%s%sroad_name = %s", LOG_PREFIX, LOG_PREFIX, lv.strval);
59 if (maps_route_maneuver_get_locale(maneuver, &lv.strval) == MAPS_ERROR_NONE)
60 LOG_PRINT("%s%slocale = %s", LOG_PREFIX, LOG_PREFIX, lv.strval);
62 maps_route_maneuver_destroy(maneuver);
64 LOG_FINISH("%s}", LOG_PREFIX);
69 static bool __maps_route_path_cb(int index, int total, maps_coordinates_h coordinates, void *user_data)
71 if (!coordinates) return false;
74 snprintf(buffer, sizeof(buffer), "path [%d/%d]", index+1, total);
76 log_coordinates("", buffer, coordinates);
77 maps_coordinates_destroy(coordinates);
81 static bool __maps_route_segment_path_cb(int index, int total, maps_coordinates_h coordinates, void *user_data)
83 if (!coordinates) return false;
86 snprintf(buffer, sizeof(buffer), "path [%d/%d]", index+1, total);
88 log_coordinates(LOG_PREFIX, buffer, coordinates);
89 maps_coordinates_destroy(coordinates);
94 static bool __maps_route_segment_cb(int index, int total, maps_route_segment_h segment, void *user_data)
97 LOG_START("segment [%d/%d] = {", index+1, total);
100 if (maps_route_segment_get_distance(segment, &distance) == MAPS_ERROR_NONE && distance > 0)
101 LOG_PRINT("%sdistance = %.2f %s", LOG_PREFIX, distance, get_distance_unit_string(g_distance_unit));
104 if (maps_route_segment_get_duration(segment, &duration) == MAPS_ERROR_NONE && duration > 0)
105 LOG_PRINT("%sduration = %ld sec", LOG_PREFIX, duration);
107 maps_coordinates_h origin;
108 if (maps_route_segment_get_origin(segment, &origin) == MAPS_ERROR_NONE) {
109 log_coordinates(LOG_PREFIX, "origin", origin);
110 maps_coordinates_destroy(origin);
113 maps_coordinates_h destination;
114 if (maps_route_segment_get_destination(segment, &destination) == MAPS_ERROR_NONE) {
115 log_coordinates(LOG_PREFIX, "destination", destination);
116 maps_coordinates_destroy(destination);
120 if (maps_route_segment_get_bounding_box(segment, &area) == MAPS_ERROR_NONE) {
121 log_area(LOG_PREFIX, area);
122 maps_area_destroy(area);
125 maps_route_segment_foreach_maneuver(segment, __maps_route_segment_maneuver_cb, user_data);
127 maps_route_segment_foreach_path(segment, __maps_route_segment_path_cb, user_data);
129 maps_route_segment_destroy(segment);
136 static bool __log_route(maps_route_h route, void *user_data)
141 if (maps_route_get_route_id(route, &lv.strval) == MAPS_ERROR_NONE)
142 LOG_PRINT("route_id = %s", lv.strval);
144 maps_coordinates_h coord;
146 if (maps_route_get_origin(route, &coord) == MAPS_ERROR_NONE) {
147 maps_coordinates_get_latitude_longitude(coord, &lat, &lon);
148 maps_coordinates_destroy(coord);
149 LOG_PRINT("origin = %f, %f", lat, lon);
151 if (maps_route_get_destination(route, &coord) == MAPS_ERROR_NONE) {
152 maps_coordinates_get_latitude_longitude(coord, &lat, &lon);
153 maps_coordinates_destroy(coord);
154 LOG_PRINT("destination = %f, %f", lat, lon);
157 if (maps_route_get_distance_unit(route, (maps_distance_unit_e*)&lv.intval) == MAPS_ERROR_NONE) {
158 g_distance_unit = lv.intval;
159 LOG_PRINT("distance_unit = '%s' (%d)", get_distance_unit_string(lv.intval), lv.intval);
162 if (maps_route_get_total_distance(route, &lv.dblval) == MAPS_ERROR_NONE)
163 LOG_PRINT("distance = %.2f %s", lv.dblval, get_distance_unit_string(g_distance_unit));
165 if (maps_route_get_total_duration(route, &lv.longval) == MAPS_ERROR_NONE)
166 LOG_PRINT("duration = %ld sec", lv.longval);
168 if (maps_route_get_transport_mode(route, (maps_route_transport_mode_e*)&lv.intval) == MAPS_ERROR_NONE)
169 LOG_PRINT("transport = %s", get_transport_string(lv.intval));
171 maps_area_h area = NULL;
172 if (maps_route_get_bounding_box(route, &area) == MAPS_ERROR_NONE) {
174 maps_area_destroy(area);
177 maps_route_foreach_path(route, __maps_route_path_cb, user_data);
179 maps_route_foreach_segment(route, __maps_route_segment_cb, user_data);
188 * query functions to use APIs
191 static bool __maps_service_search_route_cb(maps_error_e error,
192 int request_id, int index,
193 int total, maps_route_h route,
197 LOG_START_TITLE("%s [%d/%d] ", (user_data ? (char*)user_data : __FUNCTION__), MIN(index+1, total), total);
201 cleanup_callback(error);
205 __log_route(route, user_data);
206 maps_route_destroy(route);
210 if (index == total - 1)
211 cleanup_callback(error);
216 int maps_test_search_route(maps_service_h maps_svc)
219 int request_id, error;
221 maps_coordinates_h origin = NULL, dest = NULL;
222 maps_coordinates_create(52.525324, 13.369911, &origin);
223 maps_coordinates_create(52.469051, 13.441912, &dest);
225 error = maps_service_search_route(maps_svc, origin, dest, maps_pref,
226 __maps_service_search_route_cb, "Search Route", &request_id);
228 maps_coordinates_destroy(origin);
229 maps_coordinates_destroy(dest);
231 if (error != MAPS_ERROR_NONE)