553dce2ab567925d7d8e2f103c8ed4e7f4fa483f
[platform/core/location/maps-plugin-mapquest.git] / src / mapquest / mapquest_geocode.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_geocode.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 GEOCODE_URL     "https://open.mapquestapi.com/geocoding/v1/address?key=%s&inFormat=kvp&outFormat=json"
28
29 int query_geocode_within_bounds(gchar *maps_key, char *address, coords_s top_left, coords_s bottom_right, int num_results, gpointer user_data)
30 {
31         char url[1024];
32         char tmpStr[512];
33
34         if (maps_key != NULL)
35                 snprintf(tmpStr, sizeof(tmpStr), GEOCODE_URL, maps_key);
36         else
37                 snprintf(tmpStr, sizeof(tmpStr), GEOCODE_URL, "null");
38
39         strcpy(url, tmpStr);
40
41         snprintf(tmpStr, sizeof(tmpStr), "&boundingBox=%f,%f,%f,%f", top_left.latitude, top_left.longitude, bottom_right.latitude, bottom_right.longitude);
42         strcat(url, tmpStr);
43
44         snprintf(tmpStr, sizeof(tmpStr), "&maxResults=%d", num_results);
45         strcat(url, tmpStr);
46
47         snprintf(tmpStr, sizeof(tmpStr), "&location=%s", address);
48         strcat(url, tmpStr);
49
50         add_handle(url, REQ_TYPE_GEOCODE, user_data);
51
52         return 0;
53 }
54
55 int query_geocode(gchar *maps_key, char *address, int num_results, gpointer user_data)
56 {
57         MAP_DEBUG("geocode address : %s", address);
58         char url[1024];
59         char tmpStr[512];
60
61         if (maps_key != NULL)
62                 snprintf(tmpStr, sizeof(tmpStr), GEOCODE_URL, maps_key);
63         else
64                 snprintf(tmpStr, sizeof(tmpStr), GEOCODE_URL, "null");
65
66         strcpy(url, tmpStr);
67
68         snprintf(tmpStr, sizeof(tmpStr), "&maxResults=%d", num_results);
69         strcat(url, tmpStr);
70
71         snprintf(tmpStr, sizeof(tmpStr), "&location=%s", address);
72         strcat(url, tmpStr);
73
74         add_handle(url, REQ_TYPE_GEOCODE, user_data);
75
76         return 0;
77 }