2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "here_route.h"
19 HERE_PLUGIN_BEGIN_NAMESPACE
21 HereRoute::HereRoute(void *pCbFunc, void *pUserData, int nReqId)
26 m_pUserData = pUserData;
30 HereRoute::~HereRoute()
39 here_error_e HereRoute::PrepareQuery()
42 return HERE_ERROR_PERMISSION_DENIED;
44 GeoCoordinates origCoord, destCoord;
45 m_pQuery = new GeoRouteQuery(origCoord, destCoord);
48 return HERE_ERROR_OUT_OF_MEMORY;
50 return HERE_ERROR_NONE;
53 here_error_e HereRoute::PrepareWaypoint(maps_coordinates_h hOrigin, maps_coordinates_h hDestination)
56 return HERE_ERROR_OUT_OF_MEMORY;
58 if (!hOrigin || !hDestination)
59 return HERE_ERROR_INVALID_PARAMETER;
62 const int nWaypointNum = 2;
63 maps_coordinates_h hWaypointList[nWaypointNum];
64 hWaypointList[0] = hOrigin;
65 hWaypointList[1] = hDestination;
67 return PrepareWaypoint(hWaypointList, nWaypointNum);
70 here_error_e HereRoute::PrepareWaypoint(const maps_coordinates_h* hWaypointList, int nWaypointNum)
73 return HERE_ERROR_OUT_OF_MEMORY;
75 if (!hWaypointList || nWaypointNum <= 0)
76 return HERE_ERROR_INVALID_PARAMETER;
78 GeoCoordinateList hereCoordList;
79 GeoCoordinates hereCoord;
80 double dLatitude, dLongitude;
82 for (int index = 0; index < nWaypointNum; index++)
84 if (hWaypointList[index] != NULL) {
85 maps_coordinates_get_latitude(hWaypointList[index], &dLatitude);
86 maps_coordinates_get_longitude(hWaypointList[index], &dLongitude);
88 //MAPS_LOGD("Waypoint --> Lat : %f, Long : %f", dLatitude, dLongitude);
90 hereCoord = GeoCoordinates(dLatitude, dLongitude);
92 if (!HereUtils::IsValid(hereCoord))
93 return HERE_ERROR_INVALID_PARAMETER;
95 hereCoordList.push_back(hereCoord);
99 m_pQuery->SetWaypoints(hereCoordList);
101 return HERE_ERROR_NONE;
104 here_error_e HereRoute::PreparePreference(maps_preference_h hPref)
107 return HERE_ERROR_OUT_OF_MEMORY;
110 return HERE_ERROR_INVALID_PARAMETER;
113 SegmentDetail aSegmentDetail;
114 ManeuverDetail aMneuverDetail;
115 m_pQuery->SetSegmentDetail(aSegmentDetail);
116 m_pQuery->SetManeuverDetail(aMneuverDetail);
120 maps_route_transport_mode_e eTransMode;
121 if (maps_preference_get_route_transport_mode(hPref, &eTransMode) == MAPS_ERROR_NONE)
123 m_pQuery->SetTravelModes(HereUtils::Convert(eTransMode));
127 maps_route_feature_e eFeature;
128 maps_route_feature_weight_e eFeatureWeight;
129 if (maps_preference_get_route_feature(hPref, &eFeature) == MAPS_ERROR_NONE &&
130 maps_preference_get_route_feature_weight(hPref, &eFeatureWeight) == MAPS_ERROR_NONE)
132 m_pQuery->SetFeatureWeight(HereUtils::Convert(eFeature),
133 HereUtils::Convert(eFeatureWeight));
138 if (maps_preference_get(hPref, MAPS_ROUTE_RECT_AREA_TO_AVOID, &szAreaToAvoid) == MAPS_ERROR_NONE)
140 GeoBoundingBox gbBox;
141 GeoBoundingBoxList gbBoxList;
142 gbBoxList.push_back(HereUtils::Convert(szAreaToAvoid, gbBox));
143 m_pQuery->SetExcludeAreas(gbBoxList);
144 g_free(szAreaToAvoid);
148 GeoRouteQuery::RouteOptimization hereOpt;
149 maps_route_optimization_e mapsOpt;
150 if (maps_preference_get_route_optimization(hPref, &mapsOpt) == MAPS_ERROR_NONE)
154 case MAPS_ROUTE_TYPE_FASTEST: hereOpt = GeoRouteQuery::RO_FastestRoute; break;
155 case MAPS_ROUTE_TYPE_SHORTEST: hereOpt = GeoRouteQuery::RO_ShortestRoute; break;
156 default: hereOpt = GeoRouteQuery::RO_FastestRoute; break;
158 m_pQuery->SetRouteOptimization(hereOpt);
162 GeoRouteQuery::MetricSystem eMetric;
163 maps_distance_unit_e eUnit;
164 if (maps_preference_get_distance_unit(hPref, &eUnit) == MAPS_ERROR_NONE)
168 case MAPS_DISTANCE_UNIT_M: eMetric = GeoRouteQuery::DIST_metric; break;
169 case MAPS_DISTANCE_UNIT_KM: eMetric = GeoRouteQuery::DIST_metric; break;
170 default: eMetric = GeoRouteQuery::DIST_imperial; break;
172 m_pQuery->SetMetricSystem(eMetric);
176 if (maps_preference_get(hPref, MAPS_ROUTE_GEOMETRY_BOUNDING_BOX, &szViewBounds) == MAPS_ERROR_NONE)
178 GeoBoundingBox gbBox;
179 HereUtils::Convert(szViewBounds, gbBox);
180 m_pQuery->SetViewBounds(gbBox);
181 g_free(szViewBounds);
184 return HERE_ERROR_NONE;
187 here_error_e HereRoute::StartRoute(void)
190 return HERE_ERROR_OUT_OF_MEMORY;
192 m_nRestReqId = m_pQuery->Execute(*this, NULL);
194 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
197 void HereRoute::OnRouteReply(const GeoRouteReply& Reply)
199 if (m_bCanceled) // ignore call back if it was cancelled.
205 maps_route_h mapsRoute;
207 GeoRouteList hereRouteList = Reply.GetRoutes();
208 int nReplyIdx = 0, nReplyNum = hereRouteList.size();
209 GeoRouteList::iterator hereRoute;
213 ((maps_service_search_route_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND, m_nReqId,
214 0, 1, NULL, m_pUserData);
219 for (hereRoute = hereRouteList.begin() ; hereRoute != hereRouteList.end() ; hereRoute++)
221 error = (maps_error_e)maps_route_create(&mapsRoute);
223 if (error == MAPS_ERROR_NONE)
226 if (!hereRoute->GetRouteId().empty())
227 maps_route_set_route_id(mapsRoute, (char*)hereRoute->GetRouteId().c_str());
230 maps_route_set_total_distance(mapsRoute, hereRoute->GetDistance());
233 maps_route_set_total_duration(mapsRoute, hereRoute->GetTravelTime());
236 maps_route_transport_mode_e eTransportMode;
237 eTransportMode = HereUtils::Convert(hereRoute->GetTravelMode());
238 maps_route_set_transport_mode(mapsRoute, eTransportMode);
241 GeoCoordinateList herePathList = hereRoute->GetPath();
242 maps_item_list_h mapsPathList;
243 maps_coordinates_h mapsPath;
245 if (maps_item_list_create(&mapsPathList) == MAPS_ERROR_NONE)
247 GeoCoordinateList::iterator herePath;
248 for (herePath = herePathList.begin(); herePath != herePathList.end(); herePath++)
250 double dLat = herePath->GetLatitude();
251 double dLng = herePath->GetLongitude();
253 if(maps_coordinates_create(dLat, dLng, &mapsPath) == MAPS_ERROR_NONE)
255 if (herePath == herePathList.begin())
256 maps_route_set_origin(mapsRoute, mapsPath);
257 else if (herePath == herePathList.end()-1)
258 maps_route_set_destination(mapsRoute, mapsPath);
260 maps_item_list_append(mapsPathList, mapsPath, maps_coordinates_clone);
262 maps_coordinates_destroy(mapsPath);
266 if (maps_item_list_items(mapsPathList))
268 maps_route_set_path(mapsRoute, mapsPathList);
269 maps_item_list_remove_all(mapsPathList, maps_coordinates_destroy);
271 maps_item_list_destroy(mapsPathList);
275 maps_area_h hMapsArea = NULL;
276 HereUtils::Convert(hereRoute->GetBounds(), hMapsArea);
279 maps_route_set_bounding_box(mapsRoute, hMapsArea);
280 maps_area_destroy(hMapsArea);
284 ProcessSegments(mapsRoute, hereRoute->GetRouteSegmentList());
289 maps_route_destroy(mapsRoute);
294 if (((maps_service_search_route_cb)m_pCbFunc)(error, m_nReqId,
295 nReplyIdx++, nReplyNum, mapsRoute, m_pUserData) == FALSE)
300 //maps_route_destroy(mapsRoute);
304 if(nReplyIdx >= nReplyNum)
309 maps_error_e HereRoute::ProcessSegments(maps_route_h mapsRoute, const RouteSegmentList& hereSegmList)
311 maps_item_list_h mapsSegmList;
312 maps_route_segment_h mapsSegm;
315 if (hereSegmList.empty()) return MAPS_ERROR_NOT_FOUND;
317 if ((error = (maps_error_e)maps_item_list_create(&mapsSegmList)) != MAPS_ERROR_NONE)
320 RouteSegmentList::const_iterator hereSegm;
321 for (hereSegm = hereSegmList.begin() ; hereSegm != hereSegmList.end() ; hereSegm++)
323 if (maps_route_segment_create(&mapsSegm) != MAPS_ERROR_NONE) continue;
326 maps_route_segment_set_distance(mapsSegm, hereSegm->GetDistance());
329 maps_route_segment_set_duration(mapsSegm, hereSegm->GetTravelTime());
331 /* origin, destination */
332 GeoCoordinateList herePathList = hereSegm->GetPath();
333 int here_path_list_size = herePathList.size();
335 if (here_path_list_size > 0)
337 GeoCoordinates hereOrig = herePathList.at(0);
338 GeoCoordinates hereDest = herePathList.at(here_path_list_size-1);
340 maps_coordinates_h mapsOrig, mapsDest;
341 maps_coordinates_create(hereOrig.GetLatitude(),
342 hereOrig.GetLongitude(), &mapsOrig);
343 maps_coordinates_create(hereDest.GetLatitude(),
344 hereDest.GetLongitude(), &mapsDest);
345 maps_route_segment_set_origin(mapsSegm, mapsOrig);
346 maps_route_segment_set_destination(mapsSegm, mapsDest);
347 maps_coordinates_destroy(mapsOrig);
348 maps_coordinates_destroy(mapsDest);
352 ProcessManeuver(mapsSegm, hereSegm->GetManeuverList());
354 maps_item_list_append(mapsSegmList, mapsSegm, maps_route_segment_clone);
355 maps_route_segment_destroy(mapsSegm);
358 if (maps_item_list_items(mapsSegmList))
360 maps_route_set_segments(mapsRoute, mapsSegmList);
361 maps_item_list_remove_all(mapsSegmList, maps_route_segment_destroy);
363 maps_item_list_destroy(mapsSegmList);
365 return MAPS_ERROR_NONE;
368 maps_error_e HereRoute::ProcessManeuver(maps_route_segment_h mapsSegm, const ManeuverList& hereManeList)
370 maps_item_list_h mapsManeList;
371 maps_route_maneuver_h mapsManeuver;
372 maps_coordinates_h mapsCoord;
375 if (hereManeList.empty()) return MAPS_ERROR_NOT_FOUND;
377 if ((error = (maps_error_e)maps_item_list_create(&mapsManeList)) != MAPS_ERROR_NONE)
380 ManeuverList::const_iterator hereMane;
381 for (hereMane = hereManeList.begin() ; hereMane != hereManeList.end() ; hereMane++)
383 if (maps_route_maneuver_create(&mapsManeuver) != MAPS_ERROR_NONE) continue;
386 if (maps_coordinates_create(hereMane->GetPosition().GetLatitude(),
387 hereMane->GetPosition().GetLongitude(), &mapsCoord) == MAPS_ERROR_NONE)
389 maps_route_maneuver_set_position(mapsManeuver, mapsCoord);
390 maps_coordinates_destroy(mapsCoord);
394 if (!hereMane->GetInstructionText().empty())
395 maps_route_maneuver_set_instruction_text(mapsManeuver,
396 (char*)hereMane->GetInstructionText().c_str());
399 maps_route_maneuver_set_distance_to_next_instruction(mapsManeuver,
400 hereMane->GetDistanceToNextInstruction());
403 maps_route_maneuver_set_time_to_next_instruction(mapsManeuver,
404 hereMane->GetTimeToNextInstruction());
406 /* direction -> turn type */
407 maps_route_maneuver_set_turn_type(mapsManeuver,
408 HereUtils::Convert(hereMane->GetDirection()));
410 maps_item_list_append(mapsManeList, mapsManeuver, maps_route_maneuver_clone);
411 maps_route_maneuver_destroy(mapsManeuver);
414 if (maps_item_list_items(mapsManeList))
416 maps_route_segment_set_maneuvers(mapsSegm, mapsManeList);
417 maps_item_list_remove_all(mapsManeList, maps_route_maneuver_destroy);
419 maps_item_list_destroy(mapsManeList);
421 return MAPS_ERROR_NONE;
424 HERE_PLUGIN_END_NAMESPACE