4 * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7 * Genie Kim <daejins.kim@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 #include <json-glib.h>
27 #include <location-map-service.h>
28 #include <location-map-service-ext.h>
30 #include "osm-route-yours.h"
31 #include "location-osm-route.h"
35 _create_route_url (const LocationPosition *origin, const LocationPosition *destination, const GList *waypoint, const LocationMapPref *svc_pref, const LocationRoutePreference *pref)
37 gdouble flon = 0.0, flat = 0.0, tlon = 0.0, tlat = 0.0;
39 char *mode = "motorcar";
42 flon = origin->longitude;
43 flat = origin->latitude;
44 tlon = destination->longitude;
45 tlat = destination->latitude;
51 return yournavi_create_url (flat, flon, tlat, tlon, ways, 1, mode, 1);
52 //yournavi_create_url (gdouble flon, gdouble flat, gdouble tlon, gdouble tlat, const GList *ways, int type, char *mode, int instruction);
57 _position_foreach_cb (gpointer data, gpointer user_data)
61 osm_data_t *ndata = (osm_data_t *)user_data;
63 LocationPosition *pos = (LocationPosition*) data;
64 LocationPosition *duplicated = location_position_copy (pos);
66 ndata->waypoint = g_list_append(ndata->waypoint, duplicated);
72 _position_destory_cb (gpointer data)
76 LocationPosition *pos = (LocationPosition *)data;
78 location_position_free (pos);
83 _get_position_from_coordinates (JsonArray *_array, guint index, JsonNode *element, gpointer user_data)
85 GList **pos_list = (GList **)user_data;
86 gdouble lat = 0.0, lon = 0.0;
88 JsonArray *array = json_node_get_array(element);
90 lon = json_array_get_double_element (array, 0);
91 lat = json_array_get_double_element (array, 1);
92 *pos_list = g_list_append(*pos_list, location_position_new(0, lat, lon, 0.0, LOCATION_STATUS_2D_FIX));
98 convert_data_to_location_service (const char *result, int result_len, LocationPosition *origin, LocationPosition *dest, GList **route_list)
100 gboolean ret = FALSE;
102 JsonParser* parser = json_parser_new ();
103 JsonNode* node = NULL;
104 JsonObject *obj = NULL;
105 JsonObject *properties = NULL;
106 JsonArray *positions = NULL;
107 // gchar *instructions = NULL;
109 GList *pos_list = NULL;
110 LocationRoute *route = NULL;
111 gchar *str_distance = NULL, *str_duration = NULL;
112 gdouble distance = 0.0;
115 if (!result) return -1;
117 ret = json_parser_load_from_data(parser, result, result_len, &err);
119 MOD_LOGW("Fail to parser data");
123 node = json_parser_get_root(parser);
125 MOD_LOGW("Fail to parser data");
129 obj = json_node_get_object(node);
131 MOD_LOGW("Fail to parser data");
135 positions = json_object_get_array_member(obj, "coordinates");
137 json_array_foreach_element (positions, _get_position_from_coordinates, &pos_list);
141 properties = json_object_get_object_member(obj, "properties");
143 str_distance = (gchar *)json_object_get_string_member(properties, "distance");
144 MOD_LOGW("distance [%s]", str_distance);
145 // instructions = json_object_get_string_member (properties, "description");
146 str_duration = (gchar *)json_object_get_string_member (properties, "traveltime");
147 MOD_LOGW("time [%s]", str_duration);
150 distance = atof (str_distance);
151 MOD_LOGW("converted distance [%f]", distance);
152 g_free(str_distance);
156 duration = atol (str_duration);
157 MOD_LOGW("converted duration [%ld]", duration);
158 g_free( str_duration);
162 MOD_LOGW("property NULL");
165 LocationRouteStep *step = location_route_step_new ();
167 location_route_step_set_start_point (step, location_position_copy (origin));
168 location_route_step_set_end_point (step, location_position_copy (dest));
172 location_route_step_set_geometry (step, pos_list);
174 GList *step_list = NULL;
175 step_list = g_list_append (step_list, step);
178 // Route == RouteSegment
179 LocationRouteSegment *segment = location_route_segment_new();
180 location_route_segment_set_start_point (segment, location_position_copy (origin));
181 location_route_segment_set_end_point (segment, location_position_copy (dest));
182 location_route_segment_set_distance (segment, distance);
183 location_route_segment_set_duration (segment, duration);
184 location_route_segment_set_route_step (segment, step_list);
186 GList *seg_list = NULL;
187 seg_list = g_list_append (seg_list, segment);
190 route = location_route_new ();
191 location_route_set_origin(route, origin);
192 location_route_set_destination(route, dest);
193 location_route_set_total_distance(route, distance);
194 location_route_set_distance_unit(route, "M");
195 location_route_set_total_duration(route, duration);
196 location_route_set_route_segment(route, seg_list);
198 *route_list = g_list_append(*route_list, route);
204 osm_request_route (const LocationPosition *origin,
205 const LocationPosition *destination,
207 const LocationMapPref *svc_pref,
208 const LocationRoutePreference *pref,
210 const gpointer user_data,
213 osm_data_t *ndata = g_new0(osm_data_t, 1);
215 ndata->route_cb = cb;
216 ndata->user_data = (gpointer)user_data;
217 ndata->origin = location_position_copy(origin);
218 ndata->dest = location_position_copy(destination);
219 if (waypoint) g_list_foreach(waypoint, _position_foreach_cb, ndata);
221 char *url = _create_route_url (origin, destination, waypoint, svc_pref, pref);
224 return LOCATION_ERROR_UNKNOWN;
227 int ret = curl_request_route_async(url, ndata, req_id);
231 return LOCATION_ERROR_UNKNOWN;
236 return LOCATION_ERROR_NONE;
240 osm_cancel_route_request (guint req_id)
242 return curl_cancel_route_request (req_id);