Release Tizen2.0 beta
[framework/location/libslp-location.git] / tests / map-service-test.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  */
21
22 #include <glib.h>
23 #include <string.h>
24 #include <glib/gprintf.h>
25 #include <gconf/gconf-client.h>
26 #include <location-map-service.h>
27 #include "location-api-test-util.h"
28
29 #define STR_MAX 128
30 LocationMapObject* map_obj = NULL;
31 static GMainLoop *g_mainloop = NULL;
32
33 #define LOCATION_API_TEST_JSON_FILE "/opt/data/location-api-test.json"
34
35 static gpointer GmainThread(gpointer data)
36 {
37         g_mainloop = g_main_loop_new (NULL, FALSE);
38         g_printf("\n...Entering GMain Loop to Receive Notifications....\n");
39         g_main_loop_run (g_mainloop);
40         g_main_loop_unref (g_mainloop);
41         g_mainloop = NULL;
42         return NULL;
43 }
44
45
46 static void GetLocationError(char str[STR_MAX], int ret)
47 {
48         switch(ret)
49         {
50                 case LOCATION_ERROR_NONE:
51                         g_utf8_strncpy(str, "LOCATION_ERROR_NONE", STR_MAX);
52                         break;
53                 case LOCATION_ERROR_NOT_ALLOWED:
54                         g_utf8_strncpy(str, "LOCATION_ERROR_NOT_ALLOWED", STR_MAX);
55                         break;
56                 case LOCATION_ERROR_NOT_AVAILABLE:
57                         g_utf8_strncpy(str, "LOCATION_ERROR_NOT_AVAILABLE", STR_MAX);
58                         break;
59                 case LOCATION_ERROR_NETWORK_FAILED:
60                         g_utf8_strncpy(str, "LOCATION_ERROR_NETWORK_FAILED", STR_MAX);
61                         break;
62                 case LOCATION_ERROR_NETWORK_NOT_CONNECTED:
63                         g_utf8_strncpy(str, "LOCATION_ERROR_NETWORK_NOT_CONNECTED", STR_MAX);
64                         break;
65                 case LOCATION_ERROR_CONFIGURATION:
66                         g_utf8_strncpy(str, "LOCATION_ERROR_CONFIGURATION", STR_MAX);
67                         break;
68                 case LOCATION_ERROR_PARAMETER:
69                         g_utf8_strncpy(str, "LOCATION_ERROR_PARAMETER", STR_MAX);
70                         break;
71                 case LOCATION_ERROR_UNKNOWN:
72                         g_utf8_strncpy(str, "LOCATION_ERROR_UNKNOWN", STR_MAX);
73                         break;
74                 case LOCATION_ERROR_NOT_SUPPORTED:
75                         g_utf8_strncpy(str, "LOCATION_ERROR_UNKNOWN", STR_MAX);
76                         break;
77                 default:
78                         g_utf8_strncpy(str, "Error: undefined error code", STR_MAX);
79         }
80 }
81
82 static void GetAccuracyLevel(char str[STR_MAX], LocationAccuracyLevel acc_level)
83 {
84         switch (acc_level) {
85                 case LOCATION_ACCURACY_LEVEL_NONE:
86                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_NONE", STR_MAX);
87                         break;
88                 case LOCATION_ACCURACY_LEVEL_COUNTRY:
89                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_COUNTRY", STR_MAX);
90                         break;
91                 case LOCATION_ACCURACY_LEVEL_REGION:
92                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_REGION", STR_MAX);
93                         break;
94                 case LOCATION_ACCURACY_LEVEL_LOCALITY:
95                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_LOCALITY", STR_MAX);
96                         break;
97                 case LOCATION_ACCURACY_LEVEL_POSTALCODE:
98                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_POSTALCODE", STR_MAX);
99                         break;
100                 case LOCATION_ACCURACY_LEVEL_STREET:
101                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_STREET", STR_MAX);
102                         break;
103                 case LOCATION_ACCURACY_LEVEL_DETAILED:
104                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_DETAILED", STR_MAX);
105                         break;
106                 default:
107                         g_utf8_strncpy(str, "Error: undefined accuracy level", STR_MAX);
108         }
109 }
110
111 static void SelectOpt(char* buf)
112 {
113         int iLen = 0;
114         fgets(buf, 255, stdin);
115         iLen = g_utf8_strlen(buf, -1);
116         buf[iLen-1] = '\0';
117 }
118
119 static int PromptInt()
120 {
121         char buf[255];
122         int ret;
123         fgets(buf, 255, stdin);
124         buf[strlen(buf)-1]='\0';
125         ret = g_ascii_strtoll(buf, NULL, 10);
126         return ret;
127 }
128
129 static double PromptDB()
130 {
131         char buf[255];
132         double ret;
133         fgets(buf, 255, stdin);
134         buf[strlen(buf)-1]='\0';
135         ret = g_ascii_strtod(buf, NULL);
136         return ret;
137 }
138
139 static void PrintProperty (LocationObject* loc)
140 {
141         if (!loc) return;
142 #if 0
143         LocationMethod method = LOCATION_METHOD_NONE;
144         gchar method_str[STR_MAX] = {0, };
145
146         gchar* devname = NULL;
147
148         g_object_get(loc, "method", &method, NULL);
149         GetMethod(method_str, method);
150         g_printf("method[%s] ", method_str);
151
152         if (LOCATION_METHOD_GPS == method) {
153                 g_object_get(loc, "dev-name", &devname, NULL);
154                 if (devname) {
155                         g_printf("dev-name[%s] ", devname);
156                         g_free(devname);
157                 }
158         }
159 #endif
160 }
161
162 static void _print_property (gpointer data, gpointer user_data)
163 {
164         LocationLandmark *landmark = (LocationLandmark *)user_data;
165         gpointer key = (gpointer)data;
166         gpointer value = NULL;
167
168         if (key) {
169                 value = (gpointer)location_landmark_get_property(landmark, key);
170                 g_printf(", [%s:%s]", (gchar*) key, (gchar*) value);
171         }
172 }
173
174 static void
175 __plugin_print_poi_list (gpointer data, gpointer user_data)
176 {
177         LocationLandmark *landmark = (LocationLandmark *)data;
178         LocationPosition *pos = location_landmark_get_position (landmark);
179         LocationAddress *addr = location_landmark_get_address (landmark);
180
181         g_printf ("[ID:%d], [NAME:%s], phone[%s],pos[%f:%f], addr[%s:%s:%s:%s:%s]",
182                         location_landmark_get_id (landmark),
183                         location_landmark_get_name(landmark),
184                         location_landmark_get_phone_number (landmark),
185                         pos->latitude, pos->longitude,
186                         addr->building_number, addr->city, addr->country_code, addr->district,
187                         addr->postal_code);
188
189         GList *key_list = location_landmark_get_property_key(landmark);
190         if (key_list) {
191                 g_list_foreach(key_list, _print_property, landmark);
192         }
193
194         g_printf("\n");
195 }
196
197
198 static void cb_poi(LocationError error, guint req_id, GList * landmark_list, gchar * error_code, gchar * error_msg, gpointer userdata)
199 {
200         g_printf("\n===== __location_POI_cb ======\n");
201         if (error != LOCATION_ERROR_NONE) {
202                 g_printf("Failed :%d\n", error);
203                 return;
204         }
205         g_printf("Success, poi_list[0x%x] user_data[0x%x] req_id[%d]\n", (unsigned int)landmark_list, (unsigned int)userdata, req_id);
206         g_list_foreach (landmark_list, __plugin_print_poi_list, NULL);
207
208         if (error_code && error_msg) {
209                 g_printf("__location_POI_cb: error_code[%s], error_msg[%s]\n", error_code, error_msg);
210         }
211 }
212 static void PrintStepInfo(const LocationRouteStep *step)
213 {
214         const LocationPosition *start = location_route_step_get_start_point(step);
215         gdouble start_lat = 0;
216         gdouble start_lon = 0;
217         if (start) {
218                 start_lat = start->latitude;
219                 start_lon = start->longitude;
220         } else {
221                 g_printf("Step start position NULL\n");
222         }
223         const LocationPosition *end = location_route_step_get_end_point(step);
224         gdouble end_lat = 0;
225         gdouble end_lon = 0;
226         if (end) {
227                 end_lat = end->latitude;
228                 end_lon = end->longitude;
229         } else {
230                 g_printf("Step end position NULL\n");
231         }
232         const gchar *inst = location_route_step_get_instruction(step);
233
234         g_printf("Step: start(%f/%f), end(%f/%f), instruction(%s)\n", start_lat, start_lon, end_lat, end_lon, inst);
235 }
236
237 static void cb_print_route_step (gpointer data, gpointer user_data)
238 {
239         g_printf("+++Step begin\n");
240         LocationRouteStep *step = (LocationRouteStep *)data;
241
242         PrintStepInfo(step);
243         g_printf("---Step end\n");
244 }
245
246 static void cb_print_route_segment (gpointer data, gpointer user_data)
247 {
248         g_printf("++Segment begin\n");
249         LocationRouteSegment *seg = (LocationRouteSegment *)data;
250         gdouble seg_dist = location_route_segment_get_distance(seg);
251         glong seg_duration = location_route_segment_get_duration(seg);
252         const LocationPosition *start = location_route_segment_get_start_point(seg);
253         gdouble start_lat = 0;
254         gdouble start_lon = 0;
255         if (start) {
256                 start_lat = start->latitude;
257                 start_lon = start->longitude;
258         } else {
259                 g_printf("Segment start position NULL\n");
260         }
261         const LocationPosition *end = location_route_segment_get_end_point(seg);
262         gdouble end_lat = 0;
263         gdouble end_lon = 0;
264         if (end) {
265                 end_lat = end->latitude;
266                 end_lon = end->longitude;
267         } else {
268                 g_printf("Segment end postion NULL\n");
269         }
270         g_printf("Segment info: Distance[%f], Duration[%ld], start(%f/%f), end(%f/%f)\n", seg_dist, seg_duration,
271                         start_lat, start_lon, end_lat, end_lon);
272
273         GList *step_list = location_route_segment_get_route_step(seg);
274         GList *tmp_list = (GList *)step_list;
275         if (tmp_list) {
276                 g_list_foreach(tmp_list, cb_print_route_step, NULL);
277         }
278         g_printf("--Segment end\n");
279 }
280
281
282 static void cb_print_route_list (gpointer data, gpointer user_data)
283 {
284         g_printf("+Route begin\n");
285         LocationRoute *route = (LocationRoute *)data;
286
287         const LocationPosition *start = location_route_get_origin(route);
288         gdouble start_lat = 0;
289         gdouble start_lon = 0;
290         if (start) {
291                 start_lat = start->latitude;
292                 start_lon = start->longitude;
293         } else {
294                 g_printf("Route start position NULL\n");
295         }
296         const LocationPosition *end = location_route_get_destination(route);
297         gdouble end_lat = 0;
298         gdouble end_lon = 0;
299         if (end) {
300                 end_lat = end->latitude;
301                 end_lon = end->longitude;
302         } else {
303                 g_printf("Route end position NULL\n");
304         }
305         g_printf("Route: start(%f/%f), end(%f/%f)\n", start_lat, start_lon, end_lat, end_lon);
306
307         gdouble distance = location_route_get_total_distance(route);
308         const gchar *dis_unit = location_route_get_distance_unit(route);
309         glong duration = location_route_get_total_duration(route);
310         const LocationBoundary *bound = location_route_get_bounding_box(route);
311         if (bound && bound->type == LOCATION_BOUNDARY_RECT) {
312                 g_printf("RECT left top[%f-%f], right bottom[%f-%f]\n", bound->rect.left_top->latitude, bound->rect.left_top->longitude,
313                         bound->rect.right_bottom->latitude, bound->rect.right_bottom->longitude);
314         } else {
315                 g_printf("route boundary not exist, or not RECT\n");
316         }
317         g_printf ("Distance[%f], Distance unit[%s], Duration[%ld]\n", distance, dis_unit, duration);
318
319         GList *seg_list = location_route_get_route_segment(route);
320         if (seg_list) {
321                 g_list_foreach(seg_list, cb_print_route_segment, NULL);
322         }
323
324         g_printf("-Route end\n");
325 }
326
327 static void cb_route(LocationError error, guint req_id, GList * route_list, gchar * error_code, gchar * error_msg, gpointer userdata)
328 {
329         g_printf("\n===== cb_route ======\n");
330         if (error != LOCATION_ERROR_NONE) {
331                 g_printf("Failed :%d\n", error);
332                 return;
333         }
334
335         g_printf("Success, poi_list[0x%x] user_data[0x%x] req_id[%d]\n", (unsigned int)route_list, (unsigned int)userdata, req_id);
336
337         if (route_list)
338                 g_list_foreach (route_list, cb_print_route_list, NULL);
339
340         if (error_code && error_msg) {
341                 g_printf("cb_route: error_code[%s], error_msg[%s]\n", error_code, error_msg);
342         }
343 }
344
345 static void cb_address(LocationError error, LocationAddress *addr, LocationAccuracy *acc, gpointer userdata)
346 {
347         if (error != LOCATION_ERROR_NONE) {
348                 g_printf("cb_address failed: error=%d\n", error);
349                 return;
350         }
351         char str[STR_MAX];
352         g_printf("userdata[0x%x] building number: [%s], street: [%s], state: [%s], country code: [%s], city: [%s], district: [%s], postal code: [%s]\n",
353                 (unsigned int)userdata, addr->building_number, addr->street, addr->state, addr->country_code, addr->city, addr->district, addr->postal_code);
354         GetAccuracyLevel(str, acc->level);
355         g_printf("level: [%s], horizontal_accuracy: [%f], vertical_accuracy: [%f]\n", str, acc->horizontal_accuracy, acc->vertical_accuracy);
356 }
357
358 typedef struct {
359         LocationObject *obj;
360         LocationPosition *pos;
361         LocationAddress *addr;
362         gchar *str_addr;
363         LocationPositionCB pos_cb;
364         LocationAddressCB addr_cb;
365         gpointer user_data;
366 } IdleData;
367
368 static gboolean idle_address_from_position_async(gpointer data)
369 {
370         IdleData* idle_data = (IdleData*)data;
371         char str[STR_MAX];
372         int ret = location_map_get_address_from_position_async(idle_data->obj, idle_data->pos, idle_data->addr_cb, idle_data->user_data);
373         GetLocationError(str, ret);
374         g_printf("location_map_get_address_from_position_async: returned value [%s]\n", str);
375         location_position_free(idle_data->pos);
376         g_free(idle_data);
377         return FALSE;
378 }
379
380 void _print_keys(gpointer value)
381 {
382         g_printf(" %s ", (gchar*) value);
383 }
384
385 static void print_map_service_keys (LocationObject *obj, int type)
386 {
387         GList *key = NULL;
388
389         location_map_get_provider_capability_key (obj, type, &key);
390
391         if (key) {
392                 g_list_foreach (key, _print_keys, NULL);
393         }
394
395         g_list_free_full(key, g_free);
396 }
397
398 void _print_provider (gpointer data, gpointer user_data)
399 {
400         gchar *provider = (gchar *)data;
401
402         g_printf("[%s] is supported\n", provider);
403 }
404
405 static void print_menu()
406 {
407         g_printf("\n================================= Location API Test =================================\n");
408         g_printf("q.   Exit\n");
409         g_printf("1.   location_init\n");
410         g_printf("2.   location_map_new for default \n");
411         g_printf("2a.   location_map_new for decarta \n");
412         g_printf("2b.   location_map_new for osm \n");
413         g_printf("3.   location_map_free\n");
414         g_printf("4.   location_map_get_address_from_position\n");
415         g_printf("4a.  location_map_get_address_from_position_async\n");
416         g_printf("5.   location_map_search_poi_with_keyword\n");
417         g_printf("5a.  location_map_search_poi_with_category\n");
418         g_printf("5b.  location_map_search_poi_with_poi_name\n");
419         g_printf("6.   location_map_search_poi_by_area_with_keyword\n");
420         g_printf("6a.  location_map_search_poi_by_area_with_category\n");
421         g_printf("6b.  location_map_search_poi_by_area_with_poi_name\n");
422         g_printf("7.   location_map_search_poi_by_addr_with_keyword\n");
423         g_printf("7a.  location_map_search_poi_by_addr_with_category\n");
424         g_printf("7b.  location_map_search_poi_by_addr_with_poi_name\n");
425         g_printf("8.   location_map_search_poi_by_freeformed_address_with_keyword\n");
426         g_printf("8a.  location_map_search_poi_by_freeformed_address_with_category\n");
427         g_printf("8b.  location_map_search_poi_by_freeformed_address_with_poi_name\n");
428         g_printf("9.   location_map_cancel_poi_request\n");
429         g_printf("10.  location_map_request_route\n");
430         g_printf("10a. location_map_request_route for a long distance\n");
431         g_printf("11.  location_map_cancel_route_request\n");
432         g_printf("12.  location_map_is_supported_provider_capability\n");
433         g_printf("13.  location_map_get_provider_capability_key\n");
434         g_printf("14.  location_map_get_supported_providers \n");
435         g_printf("15.  location_map_get_default_provider \n");
436         g_printf("16.  location_map_set_provider \n");
437         g_printf("99. change map provider to default\n");
438         g_printf("99a. change map provider to decarta\n");
439         g_printf("99b. change map provider to osm\n");
440
441         g_printf("==================================== Property ====================================\n");
442         PrintProperty(map_obj);
443         g_printf("\n==================================================================================\n");
444 }
445
446 int main(int argc, char** argv)
447 {
448         char strOpt[255];
449         int ret;
450         char str[STR_MAX];
451         GError *gerr = NULL;
452         guint req_id = 0;
453
454         // If application is executed by AUL, this is not needed.
455         g_setenv("PKG_NAME", "org.tizen.map-service-test", 1);
456
457         g_type_init();
458
459 #if !GLIB_CHECK_VERSION (2, 31, 0)
460         if( !g_thread_supported() )
461         {
462                 g_thread_init(NULL);
463         }
464 #endif
465
466         GThread *g_main;
467         g_main = g_thread_create(GmainThread, NULL, TRUE, &gerr);
468         if (!g_main) {
469                 g_debug("Error create gmain thread: Err domain[%d] Err code[%d] Err msg[%s]",
470                         gerr->domain, gerr->code, gerr->message);
471                 g_error_free(gerr);
472                 return 0;
473         }
474
475         g_printf("--- Start LBS Test App ---\n");
476         while(1)
477         {
478                 print_menu();
479                 g_printf("Select option: ");
480                 SelectOpt(strOpt);
481                 g_printf("======================================================================================\n");
482                 if (0 == g_strcmp0 ("x", strOpt)) {
483
484                 }
485                 else if (0 == g_strcmp0("1",strOpt)) {
486                         ret = location_init();
487                         GetLocationError(str, ret);
488                         g_printf("location_init: returned value [%s]\n", str);
489                 } else if (0 == g_strcmp0("2",strOpt)) {
490                         if (map_obj) {
491                                 g_printf("Location object already existed: [0x%x]", (unsigned int)map_obj);
492                                 continue;
493                         }
494                         map_obj = location_map_new(NULL);
495                         if(map_obj) g_printf("Success\n");
496                         else             g_printf("Failed\n");
497                 } else if (0 == g_strcmp0("2a",strOpt)) {
498                         if (map_obj) {
499                                 g_printf("Location object already existed: [0x%x]", (unsigned int)map_obj);
500                                 continue;
501                         }
502                         map_obj = location_map_new("decarta");
503                         if(map_obj) g_printf("Success\n");
504                         else             g_printf("Failed\n");
505                 } else if (0 == g_strcmp0("2b",strOpt)) {
506                         if (map_obj) {
507                                 g_printf("Location object already existed: [0x%x]", (unsigned int)map_obj);
508                                 continue;
509                         }
510                         map_obj = location_map_new("osm");
511                         if(map_obj) g_printf("Success\n");
512                         else             g_printf("Failed\n");
513                 } else if (0 == g_strcmp0("3",strOpt)) {
514                         ret = location_map_free (map_obj);
515                         map_obj = NULL;
516                         GetLocationError(str, ret);
517                         g_printf("location_map_free: returned value [%s]\n", str);
518                 }else if(0 == g_strcmp0("4",strOpt) ){
519                         LocationPosition *pos = NULL;
520                         LocationAddress *addr = NULL;
521                         LocationAccuracy *acc = NULL;
522                         g_printf("[0].San jose [1].Suwon HQ [*].Custom\n");
523                         g_printf("Select Position: ");
524                         int opt = PromptInt();
525                         if(opt == 0)      pos = location_position_new(0, 37.335276, -121.890059, 0, LOCATION_STATUS_2D_FIX);
526                         else if(opt == 1) pos = location_position_new(0, 37.257809, 127.056383, 0, LOCATION_STATUS_2D_FIX);
527                         else{
528                                 g_printf("Input latitude: ");
529                                 gdouble lat = PromptDB();
530                                 g_printf("Input longitude: ");
531                                 gdouble lon = PromptDB();
532                                 pos = location_position_new(0, lat, lon, 0, LOCATION_STATUS_2D_FIX);
533                         }
534                         ret = location_map_get_address_from_position(map_obj, pos, &addr, &acc);
535                         GetLocationError(str, ret);
536                         g_printf("location_map_get_address_from_position: returned value [%s]\n", str);
537                         if(ret == LOCATION_ERROR_NONE){
538                                 g_printf("building number: [%s], street: [%s], state: [%s], country code: [%s], city: [%s], district: [%s], postal code: [%s]\n",
539                                         addr->building_number, addr->street, addr->state, addr->country_code, addr->city, addr->district, addr->postal_code);
540                         }
541                         if(addr) location_address_free(addr);
542                         if(acc) location_accuracy_free(acc);
543                 }else if(0 == g_strcmp0("4a",strOpt) ){
544                         IdleData* data = g_new0(IdleData, 1);
545                         data->obj = map_obj;
546                         data->addr_cb = cb_address;
547                         data->user_data = map_obj;
548
549                         g_printf("[0].San jose [1].Suwon HQ [*].Custom\n");
550                         g_printf("Select Position: ");
551                         int opt = PromptInt();
552                         if(opt == 0)       data->pos = location_position_new(0, 37.335276, -121.890059, 0, LOCATION_STATUS_2D_FIX);
553                         else if (opt == 1) data->pos = location_position_new(0, 37.257809, 127.056383, 0, LOCATION_STATUS_2D_FIX);
554                         else {
555                                 g_printf("Input latitude: ");
556                                 gdouble lat = PromptDB();
557                                 g_printf("Input longitude: ");
558                                 gdouble lon = PromptDB();
559                                 data->pos = location_position_new(0, lat, lon, 0, LOCATION_STATUS_2D_FIX);
560                         }
561                         g_idle_add((GSourceFunc)idle_address_from_position_async, data);
562                 }else if(0 == g_strcmp0("5", strOpt)) {
563                         g_printf("location_map_search_poi\n");
564
565                         LocationPOIFilter *filter = location_poi_filter_new();
566                         location_poi_filter_set(filter, "KEYWORD", "pizza");
567
568                         LocationPOIPreference *pref = location_poi_pref_new();
569                         location_poi_pref_set_max_result(pref, 2);
570                         location_poi_pref_set_sort_by(pref, "Distance");
571                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_DESC);
572
573                         LocationPosition *position = location_position_new(0, 37.771008, -122.41175, 0, LOCATION_STATUS_2D_FIX);
574
575                         ret = location_map_search_poi(map_obj, filter, position, pref, cb_poi, NULL, &req_id);
576                         GetLocationError(str, ret);
577                         if(ret != LOCATION_ERROR_NONE) {
578                                         g_printf("Fail to search POI. Error[%s]\n", str);
579                         } else {
580                                         g_printf("Seach POI success, req_id %d\n", req_id);
581                         }
582
583                         location_poi_filter_free(filter);
584                         location_poi_pref_free(pref);
585                         location_position_free(position);
586                 }else if(0 == g_strcmp0("5a", strOpt)) {
587                         g_printf("location_map_search_poi_with_category\n");
588                         LocationPOIFilter *filter = location_poi_filter_new();
589                         location_poi_filter_set(filter, "CATEGORY", "restaurant");
590
591                         LocationPOIPreference *pref = location_poi_pref_new();
592                         location_poi_pref_set_max_result(pref, 5);
593                         location_poi_pref_set_sort_by(pref, "Distance");
594                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);
595
596                         LocationPosition *position = location_position_new(0, 37.771008, -122.41175, 0, LOCATION_STATUS_2D_FIX);
597
598                         ret = location_map_search_poi(map_obj, filter, position, pref, cb_poi, NULL, &req_id);
599                         GetLocationError(str, ret);
600                         if(ret != LOCATION_ERROR_NONE) {
601                                         g_printf("Fail to search POI. Error[%s]\n", str);
602                         } else {
603                                         g_printf("Seach POI success, req_id %d\n", req_id);
604                         }
605
606                         location_poi_filter_free(filter);g_printf("location_search_poi_by_freeformed_address_with_poi_name\n");
607                         location_poi_pref_free(pref);
608                         location_position_free(position);
609
610                 }else if(0 == g_strcmp0("5b", strOpt)) {
611                         g_printf("location_map_search_poi_with_poi_name\n");
612
613                         LocationPOIFilter *filter = location_poi_filter_new();
614                         location_poi_filter_set(filter, "POIName", "cafe");
615
616                         LocationPOIPreference *pref = location_poi_pref_new();
617                         location_poi_pref_set_max_result(pref, 10);
618                         gchar *item = g_strdup("Distance");
619                         location_poi_pref_set_sort_by(pref, item);
620                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_DESC);
621
622                         LocationPosition *position = location_position_new(0, 37.771008, -122.41175, 0, LOCATION_STATUS_2D_FIX);
623
624                         ret = location_map_search_poi(map_obj, filter, position, pref, cb_poi, NULL, &req_id);
625                         GetLocationError(str, ret);
626                         if(ret != LOCATION_ERROR_NONE) {
627                                         g_printf("Fail to search POI. Error[%s]\n", str);
628                         } else {
629                                         g_printf("Seach POI success, req_id %d\n", req_id);
630                         }
631
632                         location_poi_filter_free(filter);
633                         location_poi_pref_free(pref);
634                         location_position_free(position);
635
636                 }else if(0 == g_strcmp0("6", strOpt)) {
637                         g_printf("location_map_search_poi_by_area_with_keyword\n");
638
639                         // only circle supported by decarta
640                         LocationPosition *center = location_position_new(0, 37.336723, -121.889555, 0, LOCATION_STATUS_2D_FIX);
641                         gdouble radius = 400;
642                         LocationBoundary *bound = location_boundary_new_for_circle(center, radius);
643
644                         LocationPOIFilter *filter = location_poi_filter_new();
645                         gchar *key = g_strdup("KEYWORD");
646                         gchar *value = g_strdup("cafe");
647                         location_poi_filter_set(filter, key, value); // same with Type ?  CATEGORY, KEYWORD, POIName
648
649                         LocationPOIPreference *pref = location_poi_pref_new();
650                         location_poi_pref_set_max_result(pref, 10);
651                         gchar *item = g_strdup("Distance");
652                         location_poi_pref_set_sort_by(pref, item); // can't set const char* directly !!
653                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);  //LOCATION_POI_PREF_SO_ASC
654
655
656                         ret = location_map_search_poi_by_area(map_obj, filter, bound, pref, cb_poi, NULL, &req_id);
657                         GetLocationError(str, ret);
658                         if (ret != LOCATION_ERROR_NONE) {
659                                         g_printf("Fail to search POI by area. Error[%s]\n", str);
660                         } else {
661                                         g_printf("Seach POI by area sucess, req_id %d\n", req_id);
662                         }
663
664                         g_free(key);
665                         g_free(value);
666                         g_free(item);
667                         location_position_free(center);
668                         location_boundary_free(bound);
669                         location_poi_filter_free(filter);
670                         location_poi_pref_free(pref);
671                 }else if(0 == g_strcmp0("6a", strOpt)) {
672                         g_printf("location_map_search_poi_by_area_with_category\n");
673
674                         // only circle supported by decarta
675                         LocationPosition *center = location_position_new(0, 37.336723, -121.889555, 0, LOCATION_STATUS_2D_FIX);
676                         gdouble radius = 400;
677                         LocationBoundary *bound = location_boundary_new_for_circle(center, radius);
678
679                         LocationPOIFilter *filter = location_poi_filter_new();
680                         gchar *key = g_strdup("CATEGORY");
681                         gchar *value = g_strdup("restaurant");
682                         location_poi_filter_set(filter, key, value);
683
684                         LocationPOIPreference *pref = location_poi_pref_new();
685                         location_poi_pref_set_max_result(pref, 5);
686                         gchar *item = g_strdup("Distance");
687                         location_poi_pref_set_sort_by(pref, item);
688                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);
689
690                         ret = location_map_search_poi_by_area(map_obj, filter, bound, pref, cb_poi, NULL, &req_id);
691                         GetLocationError(str, ret);
692                         if (ret != LOCATION_ERROR_NONE) {
693                                         g_printf("Fail to search POI by area. Error[%s]\n", str);
694                         } else {
695                                         g_printf("Seach POI by area sucess, req_id %d\n", req_id);
696                         }
697
698                         g_free(key);
699                         g_free(value);
700                         g_free(item);
701                         location_position_free(center);
702                         location_boundary_free(bound);
703                         location_poi_filter_free(filter);
704                         location_poi_pref_free(pref);
705
706                 }else if(0 == g_strcmp0("6b", strOpt)) {
707                         g_printf("location_map_search_poi_by_area_with_poi_name\n");
708                         
709                         // only circle supported by decarta
710                         LocationPosition *center = location_position_new(0, 37.336723, -121.889555, 0, LOCATION_STATUS_2D_FIX);
711                         gdouble radius = 400;
712                         LocationBoundary *bound = location_boundary_new_for_circle(center, radius);
713
714                         LocationPOIFilter *filter = location_poi_filter_new();
715                         gchar *key = g_strdup("POIName");
716                         gchar *value = g_strdup("cafe");
717                         location_poi_filter_set(filter, key, value); // same with Type ?  CATEGORY, KEYWORD, POIName
718
719                         LocationPOIPreference *pref = location_poi_pref_new();
720                         location_poi_pref_set_max_result(pref, 10);
721                         gchar *item = g_strdup("Distance");
722                         location_poi_pref_set_sort_by(pref, item); // can't set const char* directly !!
723                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);  //LOCATION_POI_PREF_SO_ASC
724
725
726                         ret = location_map_search_poi_by_area(map_obj, filter, bound, pref, cb_poi, NULL, &req_id);
727                         GetLocationError(str, ret);
728                         if (ret != LOCATION_ERROR_NONE) {
729                                         g_printf("Fail to search POI by area. Error[%s]\n", str);
730                         } else {
731                                         g_printf("Seach POI by area sucess, req_id %d\n", req_id);
732                         }
733
734                         g_free(key);
735                         g_free(value);
736                         g_free(item);
737                         location_position_free(center);
738                         location_boundary_free(bound);
739                         location_poi_filter_free(filter);
740                         location_poi_pref_free(pref);
741
742                 }else if(0 == g_strcmp0("7", strOpt)) {
743                         g_printf("location_map_search_poi_by_address_with_keyword\n");
744
745                         LocationAddress *addr = location_address_new("51", "N SAN PEDRO ST", NULL, "SAN JOSE", "SANTA CLARA", "CA", "95110");
746
747                         LocationPOIFilter *filter = location_poi_filter_new();
748                         gchar *key = g_strdup("KEYWORD");
749                         gchar *value = g_strdup("cafe");
750                         location_poi_filter_set(filter, key, value);
751
752                         LocationPOIPreference *pref = location_poi_pref_new();
753                         location_poi_pref_set_max_result(pref, 10);
754                         gchar *item = g_strdup("Distance");
755                         location_poi_pref_set_sort_by(pref, item);
756                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);
757
758                         ret = location_map_search_poi_by_address(map_obj, filter, addr, pref, cb_poi, NULL, &req_id);
759                         GetLocationError(str, ret);
760                         if(ret != LOCATION_ERROR_NONE) {
761                                         g_printf("Fail to search POI by address. Error[%s]\n", str);
762                         } else {
763                                         g_printf("Seach POI by address sucess, req_id %d\n", req_id);
764                         }
765
766                         g_free(key);
767                         g_free(value);
768                         g_free(item);
769
770                         location_address_free(addr);
771                         location_poi_filter_free(filter);
772                         location_poi_pref_free(pref);
773                 }else if(0 == g_strcmp0("7a", strOpt)) {
774                         g_printf("location_search_poi_by_address_with_category\n");
775
776                         LocationAddress *addr = location_address_new("51", "N SAN PEDRO ST", NULL, "SAN JOSE", "SANTA CLARA", "CA", "95110");
777
778                         LocationPOIFilter *filter = location_poi_filter_new();
779                         location_poi_filter_set(filter, "CATEGORY", "restaurant");
780
781                         LocationPOIPreference *pref = location_poi_pref_new();
782                         location_poi_pref_set_max_result(pref, 10);
783                         location_poi_pref_set_sort_by(pref, "Distance");
784                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);
785
786                         ret = location_map_search_poi_by_address(map_obj, filter, addr, pref, cb_poi, NULL, &req_id);
787                         GetLocationError(str, ret);
788                         if(ret != LOCATION_ERROR_NONE) {
789                                         g_printf("Fail to search POI by address. Error[%s]\n", str);
790                         } else {
791                                         g_printf("Seach POI by address sucess, req_id %d\n", req_id);
792                         }
793
794                         location_address_free(addr);
795                         location_poi_filter_free(filter);
796                         location_poi_pref_free(pref);
797                 }else if(0 == g_strcmp0("7b", strOpt)) {
798                         g_printf("location_map_search_poi_by_address_with_poi_name\n");
799
800                         LocationAddress *addr = location_address_new("51", "N SAN PEDRO ST", NULL, "SAN JOSE", "SANTA CLARA", "CA", "95110");
801
802                         LocationPOIFilter *filter = location_poi_filter_new();
803                         gchar *key = g_strdup("POIName");
804                         gchar *value = g_strdup("cafe");
805                         location_poi_filter_set(filter, key, value);
806
807                         LocationPOIPreference *pref = location_poi_pref_new();
808                         location_poi_pref_set_max_result(pref, 10);
809                         gchar *item = g_strdup("Distance");
810                         location_poi_pref_set_sort_by(pref, item);
811                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);
812
813                         ret = location_map_search_poi_by_address(map_obj, filter, addr, pref, cb_poi, NULL, &req_id);
814                         GetLocationError(str, ret);
815                         if(ret != LOCATION_ERROR_NONE) {
816                                         g_printf("Fail to search POI by address. Error[%s]\n", str);
817                         } else {
818                                         g_printf("Seach POI by address sucess, req_id %d\n", req_id);
819                         }
820
821                         g_free(key);
822                         g_free(value);
823                         g_free(item);
824
825                         location_address_free(addr);
826                         location_poi_filter_free(filter);
827                         location_poi_pref_free(pref);
828
829                 }else if(0 == g_strcmp0("8", strOpt)) {
830                         g_printf("location_map_search_poi_by_freeformed_address_with_keyword\n");
831
832                         gchar *addr = g_strdup("North Second St.");
833
834                         LocationPOIFilter *filter = location_poi_filter_new();
835                         location_poi_filter_set(filter, "KEYWORD", "station");
836
837                         LocationPOIPreference *pref = location_poi_pref_new();
838                         location_poi_pref_set_max_result(pref, 10);
839                         gchar *item = g_strdup("Distance");
840                         location_poi_pref_set_sort_by(pref, item); // can't set const char* directly !!
841                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);  //LOCATION_POI_PREF_SO_ASC
842
843                         ret = location_map_search_poi_by_freeformed_address(map_obj, filter, addr, pref, cb_poi, NULL, &req_id);
844                         GetLocationError(str, ret);
845                         if (ret != LOCATION_ERROR_NONE) {
846                                         g_printf("Fail to search POI by address. Error[%s]\n", str);
847                         } else {
848                                         g_printf("Seach POI by address success, req_id %d\n", req_id);
849                 }
850
851                         g_free(item);
852
853                         g_free(addr);
854                         location_poi_filter_free(filter);
855                         location_poi_pref_free(pref);
856                 }else if(0 == g_strcmp0("8a", strOpt)) {
857                         g_printf("location_map_search_poi_by_freeformed_address_with_category\n");
858                 
859                         gchar *addr = g_strdup("North Second St.");
860
861                         LocationPOIFilter *filter = location_poi_filter_new();
862                         gchar *key = g_strdup("CATEGORY");
863                         gchar *value = g_strdup("restaurant");
864                         location_poi_filter_set(filter, key, value);
865
866                         LocationPOIPreference *pref = location_poi_pref_new();
867                         location_poi_pref_set_max_result(pref, 10);
868                         gchar *item = g_strdup("Distance");
869                         location_poi_pref_set_sort_by(pref, item);
870                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);
871                         
872                         ret = location_map_search_poi_by_freeformed_address(map_obj, filter, addr, pref, cb_poi, NULL, &req_id);
873                         GetLocationError(str, ret);
874                         if (ret != LOCATION_ERROR_NONE) {
875                                         g_printf("Fail to search POI by address. Error[%s]\n", str);
876                         } else {
877                                         g_printf("Seach POI by address success, req_id %d\n", req_id);
878                 }
879
880                         g_free(key);
881                         g_free(value);
882                         g_free(item);
883
884                         g_free(addr);
885                         location_poi_filter_free(filter);
886                         location_poi_pref_free(pref);
887                 }else if(0 == g_strcmp0("8b", strOpt)) {
888                         g_printf("location_map_search_poi_by_freeformed_address_with_poi_name\n");
889
890                         gchar *addr = g_strdup("North Second St.");
891
892                         LocationPOIFilter *filter = location_poi_filter_new();
893                         location_poi_filter_set(filter, "POIName", "pizza");
894
895                         LocationPOIPreference *pref = location_poi_pref_new();
896                         location_poi_pref_set_max_result(pref, 10);
897                         location_poi_pref_set_sort_by(pref, "Distance");
898                         location_poi_pref_set_sort_order(pref, LOCATION_POI_PREF_SO_ASC);
899
900                         ret = location_map_search_poi_by_freeformed_address(map_obj, filter, addr, pref, cb_poi, NULL, &req_id);
901                         GetLocationError(str, ret);
902                         if (ret != LOCATION_ERROR_NONE) {
903                                         g_printf("Fail to search POI by address. Error[%s]\n", str);
904                         } else {
905                                         g_printf("Seach POI by address success, req_id %d\n", req_id);
906                         }
907
908                         g_free(addr);
909                         location_poi_filter_free(filter);
910                         location_poi_pref_free(pref);
911
912                 }else if(0 == g_strcmp0("9", strOpt)) {
913                         int req_id;
914
915                         g_printf("Input ReqID : ");
916                         scanf("%d", &req_id);
917
918                         ret = location_map_cancel_poi_request(map_obj, req_id);
919                         GetLocationError(str, ret);
920                         if (ret != LOCATION_ERROR_NONE) {
921                                 g_printf("Fail to cancel POI request. Error[%s]\n", str);
922                         }
923                         else
924                                 g_printf("location_map_cancel_poi_request, req_id %d\n", req_id);
925                 }else if(0 == g_strcmp0("10", strOpt)) {
926                         g_printf("location_map_request_route\n\n");
927
928                         LocationPosition *origin = location_position_new(0, 37.564263, 126.974676, 0, LOCATION_STATUS_2D_FIX);  // Seoul city hall
929                         LocationPosition *destination = location_position_new(0, 37.557120, 126.992410, 0, LOCATION_STATUS_2D_FIX);     // NamSan
930
931                         GList *waypoint = NULL;
932                         LocationPosition *via_pos = location_position_new(0, 37.560950, 126.986240, 0, LOCATION_STATUS_2D_FIX); // Wangsimli
933                         waypoint = g_list_append (waypoint, (gpointer)via_pos);
934
935                         LocationRoutePreference *pref = location_route_pref_new();
936                         location_route_pref_set_route_type(pref, "FASTEST");
937
938                         ret = location_map_request_route(map_obj, origin, destination, waypoint, pref, cb_route, NULL, &req_id);
939                         GetLocationError(str, ret);
940                         if (ret != LOCATION_ERROR_NONE) {
941                                         g_printf("Fail to search route by address. Error[%s]\n", str);
942                         } else {
943                                         g_printf("Search Route successfully, req_id %d\n", req_id);
944                         }
945                 }else if(0 == g_strcmp0("10a", strOpt)) {
946                         g_printf("location_map_request_route for a long distance\n");
947
948                         LocationPosition *origin = location_position_new(0, 29.783449,-95.373688, 0, LOCATION_STATUS_2D_FIX);
949                         LocationPosition *destination = location_position_new(0, 39.749962,-104.984665, 0, LOCATION_STATUS_2D_FIX);
950
951                         GList *waypoint = NULL;
952
953                         LocationRoutePreference *pref = location_route_pref_new();
954                         location_route_pref_set_route_type(pref, "FASTEST");
955
956                         ret = location_map_request_route(map_obj, origin, destination, waypoint, pref, cb_route, NULL, &req_id);
957                         GetLocationError(str, ret);
958                         if (ret != LOCATION_ERROR_NONE) {
959                                         g_printf("Fail to search route by address. Error[%s]\n", str);
960                         } else {
961                                         g_printf("Search Route successfully, req_id %d\n", req_id);
962                         }
963
964
965                 }else if(0 == g_strcmp0("11", strOpt)) {
966                         g_printf("location_map_cancel_route_request\n");
967
968                         int req_id;
969                         g_printf("Input ReqID : ");
970                         scanf("%d", &req_id);
971
972                         ret = location_map_cancel_route_request(map_obj, req_id);
973                         GetLocationError(str, ret);
974                         if (ret != LOCATION_ERROR_NONE) {
975                                 g_printf("Fail to cancel route request. Error[%s]\n", str);
976                         }
977                         else {
978                                 g_printf("location_map_cancel_route_request, req_id %d\n", req_id);
979                         }
980
981                 }else if (0 == g_strcmp0 ("12", strOpt)) {
982                         int idx = 0;
983                         for (idx = 0; idx < MAP_SERVICE_TYPE_MAX; idx++) {
984                                 g_printf("[%d:%d], ", idx, location_map_is_supported_provider_capability (map_obj, idx));
985                         }
986                 }else if (0 == g_strcmp0 ("13", strOpt)) {
987                         int idx = 0;
988                         for (idx = 0; idx < MAP_SERVICE_TYPE_MAX; idx++) {
989                                 g_printf("[%d:", idx);
990                                 print_map_service_keys(map_obj, idx);
991                                 g_printf("]\n");
992                         }
993                 }else if (0 == g_strcmp0 ("14", strOpt)) {
994                         GList *list = location_map_get_supported_providers (map_obj);
995                         if (list) {
996                                 g_list_foreach (list, _print_provider, NULL);
997                                 g_list_free_full (list, g_free);
998                         }
999                         else {
1000                                 g_printf("Fail to get supported_providers\n");
1001                         }
1002                 }else if (0 == g_strcmp0 ("15", strOpt)) {
1003                         gchar *default_provider = location_map_get_default_provider (map_obj);
1004                         if (default_provider) {
1005                                 g_printf("Defaut : [%s]\n", default_provider);
1006                                 g_free (default_provider);
1007                         }
1008                         else {
1009                                 g_printf("Fail to get default provider\n");
1010                         }
1011                 }else if (0 == g_strcmp0 ("16", strOpt)) {
1012                         g_printf("[0].default [1].decarta [2].osm\n");
1013                         g_printf("Select provider: ");
1014                         int opt = PromptInt();
1015                         gboolean ret = FALSE;
1016                         switch (opt)
1017                         {
1018                                 case 0:
1019                                         ret = location_map_set_provider (map_obj, NULL);
1020                                         break;
1021                                 case 1:
1022                                         ret = location_map_set_provider (map_obj, "decarta");
1023                                         break;
1024                                 case 2:
1025                                         ret = location_map_set_provider (map_obj, "osm");
1026                                         break;
1027                                 default:
1028                                         break;
1029                         }
1030
1031                         if (ret == TRUE) {
1032                                 g_printf ("Success to set provider [%d]\n", opt);
1033                         }
1034                         else {
1035                                 g_printf ("Fail to set provider [%d]\n", opt);
1036                         }
1037                 }else if (0 == g_strcmp0 ("99", strOpt)) {
1038                         if (map_obj) {
1039                                 g_object_set (map_obj, "provider", NULL, NULL);
1040                         }
1041                 }else if (0 == g_strcmp0 ("99a", strOpt)) {
1042                         if (map_obj) {
1043                                 g_object_set (map_obj, "provider", "decarta", NULL);
1044                         }
1045                 }else if (0 == g_strcmp0 ("99b", strOpt)) {
1046                         if (map_obj) {
1047                                 g_object_set (map_obj, "provider", "osm",  NULL);
1048                         }
1049                 }else if(0 == g_strcmp0("q",strOpt) ){
1050                         g_main_loop_quit(g_mainloop);
1051                         break;
1052                 }
1053         }
1054         g_thread_join(g_main);
1055         g_printf("\n--- Exit LBS Test App ---\n");
1056         return 1;
1057 }