52a05bcbe16f369a9bb5a2ae6a0ea2d03a2dcfe2
[platform/core/location/maps-plugin-mapquest.git] / src / mapquest / mapquest_place.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 <stdlib.h>
19 #include <glib.h>
20 #include <pthread.h>
21 #include "mapquest_place.h"
22 #include "mapquest_types.h"
23 #include "mapquest_server_private.h"
24 #include "mapquest_debug.h"
25 #include "mapquest_queue.h"
26 #include "mapquest_restcurl.h"
27 #include "mapquest_util.h"
28
29 #define PLACES_URL      "https://open.mapquestapi.com/nominatim/v1/search.php?format=json"
30
31 int query_places(gchar *maps_key, gchar *search_str, mapquest_boundary_s *boundary, int num_res, gpointer user_data)
32 {
33         char url[1024];
34         char tmpStr[512];
35
36         strcpy(url, PLACES_URL);
37
38         if (search_str != NULL) {
39                 snprintf(tmpStr, sizeof(tmpStr), "&q=%s", search_str);
40                 strncat(url, tmpStr, sizeof(url)-strlen(url)-1);
41         }
42
43         if (boundary != NULL) {
44                 if (boundary->type == MAPQUEST_BOUNDARY_RECT) {
45                         snprintf(tmpStr, sizeof(tmpStr), "&viewbox=%f,%f,%f,%f", boundary->rect.top_left.longitude, boundary->rect.top_left.latitude, boundary->rect.bottom_right.longitude, boundary->rect.bottom_right.latitude);
46
47                         strncat(url, tmpStr, sizeof(url)-strlen(url)-1);
48                 } else if (boundary->type == MAPQUEST_BOUNDARY_CIRCLE) {
49                         coords_s *top_left = NULL, *bottom_right = NULL;
50                         coords_s circle = boundary->circle.center;
51                         gdouble radius = (boundary->circle.radius) * 0.001;
52
53                         /* Calculate the top left coordinate of bounding box. */
54                         calculate_point(circle.latitude, circle.longitude, 315, radius, &top_left);
55
56                         /* Calculate the bottom right coordinate of bounding box. */
57                         calculate_point(circle.latitude, circle.longitude, 135, radius, &bottom_right);
58
59                         if (top_left && bottom_right) {
60                                 snprintf(tmpStr, sizeof(tmpStr), "&viewbox=%f,%f,%f,%f", top_left->longitude, top_left->latitude, bottom_right->longitude, bottom_right->latitude);
61                                 strncat(url, tmpStr, sizeof(url)-strlen(url)-1);
62                         }
63
64                         if (top_left) {
65                                 g_free(top_left);
66                                 top_left = NULL;
67                         }
68                         if (bottom_right) {
69                                 g_free(bottom_right);
70                                 bottom_right = NULL;
71                         }
72                 }
73
74                 strcat(url, "&bounded=1");
75         }
76
77         strcat(url, "&addressdetails=1");
78
79         if (num_res > 0) {
80                 snprintf(tmpStr, sizeof(tmpStr), "&limit=%d", num_res);
81                 strcat(url, tmpStr);
82         }
83
84         add_handle(url, REQ_TYPE_PLACES, user_data);
85
86         return 0;
87 }