a4ec22517bb9dc8ad3af9244c4f49c38427f3b02
[platform/core/location/maps-plugin-mapquest.git] / src / mapquest / mapquest_route.c
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <glib.h>
19 #include <pthread.h>
20 #include "mapquest_route.h"
21 #include "mapquest_types.h"
22 #include "mapquest_server_private.h"
23 #include "mapquest_debug.h"
24 #include "mapquest_queue.h"
25 #include "mapquest_restcurl.h"
26
27 #define ROUTE_URL       "https://open.mapquestapi.com/directions/v2/route?key=%s&ambiguities=ignore&outFormat=json&shapeFormat=raw&generalize=0"
28
29 int query_route(gchar *maps_key, coords_s startPoint, coords_s endPoint, route_type type, route_feature_avoids avoids, route_driving_style style, GList *waypoints, gpointer user_data)
30 {
31         char url[1024];
32         char tmpStr[512];
33
34         if (maps_key != NULL)
35                 snprintf(tmpStr, sizeof(tmpStr), ROUTE_URL, maps_key);
36         else
37                 snprintf(tmpStr, sizeof(tmpStr), ROUTE_URL, "null");
38
39         strcpy(url, tmpStr);
40
41         strcat(url, "&unit=m"); /* Keeping default as miles and conversion will be done later */
42
43         if (type == ROUTE_TYPE_FASTEST)
44                 strcat(url, "&routeType=fastest");
45         else if (type == ROUTE_TYPE_SHORTEST)
46                 strcat(url, "&routeType=shortest");
47         else if (type == ROUTE_TYPE_PEDESTRIAN)
48                 strcat(url, "&routeType=pedestrian");
49         else if (type == ROUTE_TYPE_MULTIMODAL)
50                 strcat(url, "&routeType=multimodal");
51         else if (type == ROUTE_TYPE_BICYCLE)
52                 strcat(url, "&routeType=bicycle");
53
54         if (avoids == ROUTE_AVOID_LIMITED_ACCESS)
55                 strcat(url, "&avoids=Limited Access");
56         else if (avoids == ROUTE_AVOID_TOLL_ROAD)
57                 strcat(url, "&avoids=Tollroad");
58         else if (avoids == ROUTE_AVOID_FERRY)
59                 strcat(url, "&avoids=Ferry");
60         else if (avoids == ROUTE_AVOID_UNPAVED)
61                 strcat(url, "&avoids=Unpaved");
62         else if (avoids == ROUTE_AVOID_SEASONAL_CLOSURE)
63                 strcat(url, "&avoids=Approximate Seasonal Closure");
64         else if (avoids == ROUTE_AVOID_COUNTRY_BORDER_CROSSING)
65                 strcat(url, "&avoids=Country border crossing");
66
67         if (style == DRIVING_STYLE_NORMAL)
68                 strcat(url, "&drivingStyle=2");
69         else if (style == DRIVING_STYLE_CAUTIOUS)
70                 strcat(url, "&drivingStyle=1");
71         else if (style == DRIVING_STYLE_AGGRESSIVE)
72                 strcat(url, "&drivingStyle=3");
73
74         int length = g_list_length(waypoints);
75         if (length != 0) {
76                 int index = 0;
77                 GList *waypoints_list = NULL;
78                 waypoints_list = g_list_first(waypoints);
79
80                 while (waypoints_list) {
81
82                         coords_s *data = (coords_s *) waypoints_list->data;
83
84                         if (data) {
85                                 if (index == 0)
86                                         snprintf(tmpStr, sizeof(tmpStr), "&from=%f,%f", data->latitude, data->longitude);
87                                 else
88                                         snprintf(tmpStr, sizeof(tmpStr), "&to=%f,%f", data->latitude, data->longitude);
89                                 strcat(url, tmpStr);
90                         }
91
92                         waypoints_list = g_list_next(waypoints_list);
93                         index++;
94                 }
95         } else {
96                 snprintf(tmpStr, sizeof(tmpStr), "&from=%f,%f", startPoint.latitude, startPoint.longitude);
97                 strcat(url, tmpStr);
98
99                 snprintf(tmpStr, sizeof(tmpStr), "&to=%f,%f", endPoint.latitude, endPoint.longitude);
100                 strcat(url, tmpStr);
101         }
102
103         add_handle(url, REQ_TYPE_ROUTE, user_data);
104
105         return 0;
106 }