2 * Copyright (c) 2011 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.
20 #include <geocoder_private.h>
26 #define LOG_TAG "TIZEN_N_GEOCODER"
31 #define GEOCODER_CHECK_CONDITION(condition,error,msg) \
32 if(condition) {} else \
33 { LOGE("[%s] %s(0x%08x)",__FUNCTION__, msg,error); return error;}; \
35 #define GEOCODER_NULL_ARG_CHECK(arg) \
36 GEOCODER_CHECK_CONDITION(arg != NULL,GEOCODER_ERROR_INVALID_PARAMETER,"GEOCODER_ERROR_INVALID_PARAMETER")
39 * Internal Implementation
44 geocoder_get_address_cb callback;
45 }__addr_callback_data;
49 geocoder_get_position_cb callback;
52 static int __convert_error_code(int code, char* func_name)
55 char* msg = "GEOCODER_ERROR_NONE";
58 case LOCATION_ERROR_NONE:
59 ret = GEOCODER_ERROR_NONE;
60 msg = "GEOCODER_ERROR_NONE";
62 case LOCATION_ERROR_NETWORK_FAILED:
63 case LOCATION_ERROR_NETWORK_NOT_CONNECTED:
64 ret = GEOCODER_ERROR_NETWORK_FAILED;
65 msg = "GEOCODER_ERROR_NETWORK_FAILED";
67 case LOCATION_ERROR_PARAMETER:
68 ret = GEOCODER_ERROR_INVALID_PARAMETER;
69 msg = "GEOCODER_ERROR_INVALID_PARAMETER";
71 case LOCATION_ERROR_NOT_FOUND:
72 ret = GEOCODER_ERROR_NOT_FOUND;
73 msg = "GEOCODER_ERROR_NOT_FOUND";
75 case LOCATION_ERROR_NOT_ALLOWED:
76 case LOCATION_ERROR_NOT_AVAILABLE:
77 case LOCATION_ERROR_CONFIGURATION:
78 case LOCATION_ERROR_UNKNOWN:
80 msg = "GEOCODER_ERROR_SERVICE_NOT_AVAILABLE";
81 ret = GEOCODER_ERROR_SERVICE_NOT_AVAILABLE;
83 LOGE("[%s] %s(0x%08x) : core fw error(0x%x)",func_name,msg, ret, code);
87 static void __cb_address_from_position (LocationError error, LocationAddress *addr, LocationAccuracy *acc, gpointer userdata)
89 __addr_callback_data * callback = (__addr_callback_data*)userdata;
90 if( callback == NULL || callback->callback == NULL)
92 LOGI("[%s] callback is NULL )",__FUNCTION__);
96 if(error != LOCATION_ERROR_NONE || addr == NULL)
98 callback->callback(__convert_error_code(error,(char*)__FUNCTION__), NULL, NULL, NULL, NULL, NULL, NULL, NULL, callback->data);
102 LOGI("[%s] Address - building number: %s, postal code: %s, street: %s, city: %s, district: %s, state: %s, country code: %s", __FUNCTION__ , addr->building_number, addr->postal_code, addr->street, addr->city, addr->district, addr->state, addr->country_code);
103 callback->callback(GEOCODER_ERROR_NONE, addr->building_number, addr->postal_code, addr->street, addr->city, addr->district, addr->state, addr->country_code, callback->data);
108 static void __cb_position_from_address (LocationError error, GList *position_list, GList *accuracy_list, gpointer userdata)
110 __pos_callback_data * callback = (__pos_callback_data*)userdata;
111 if( callback == NULL || callback->callback == NULL)
113 LOGI("[%s] callback is NULL )",__FUNCTION__);
117 if(error != LOCATION_ERROR_NONE || position_list == NULL || position_list->data ==NULL || accuracy_list==NULL )
119 callback->callback(__convert_error_code(error,(char*)__FUNCTION__), 0, 0, callback->data);
125 LocationPosition *pos = position_list->data;
126 if ( callback->callback(GEOCODER_ERROR_NONE, pos->latitude, pos->longitude, callback->data) != TRUE )
128 LOGI("[%s] User quit the loop ", __FUNCTION__);
131 position_list = g_list_next(position_list);
138 * Public Implementation
140 int geocoder_create(geocoder_h* geocoder)
142 GEOCODER_NULL_ARG_CHECK(geocoder);
143 if(location_init()!=LOCATION_ERROR_NONE)
145 LOGE("[%s] GEOCODER_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : fail to location_init", __FUNCTION__, GEOCODER_ERROR_SERVICE_NOT_AVAILABLE);
146 return GEOCODER_ERROR_SERVICE_NOT_AVAILABLE;
149 geocoder_s *handle = (geocoder_s*)malloc(sizeof(geocoder_s));
152 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, GEOCODER_ERROR_OUT_OF_MEMORY);
153 return GEOCODER_ERROR_OUT_OF_MEMORY;
156 memset(handle, 0 , sizeof(geocoder_s));
158 handle->object = location_map_new(NULL);
159 if(handle->object == NULL)
162 LOGE("[%s] GEOCODER_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : fail to location_map_new", __FUNCTION__, GEOCODER_ERROR_SERVICE_NOT_AVAILABLE);
163 return GEOCODER_ERROR_SERVICE_NOT_AVAILABLE;
166 *geocoder = (geocoder_h)handle;
167 return GEOCODER_ERROR_NONE;
170 int geocoder_destroy(geocoder_h geocoder)
172 GEOCODER_NULL_ARG_CHECK(geocoder);
173 geocoder_s *handle = (geocoder_s*)geocoder;
175 int ret = location_map_free(handle->object);
176 if(ret!=GEOCODER_ERROR_NONE)
178 return __convert_error_code(ret,(char*)__FUNCTION__);
181 return GEOCODER_ERROR_NONE;
184 int geocoder_get_address_from_position(geocoder_h geocoder, double latitude, double longitude, geocoder_get_address_cb callback, void *user_data)
186 GEOCODER_NULL_ARG_CHECK(geocoder);
187 GEOCODER_NULL_ARG_CHECK(callback);
188 GEOCODER_CHECK_CONDITION(latitude>=-90 && latitude<=90 ,GEOCODER_ERROR_INVALID_PARAMETER,"GEOCODER_ERROR_INVALID_PARAMETER");
189 GEOCODER_CHECK_CONDITION(longitude>=-180 && longitude<=180,GEOCODER_ERROR_INVALID_PARAMETER,"GEOCODER_ERROR_INVALID_PARAMETER");
191 geocoder_s *handle = (geocoder_s*)geocoder;
193 LocationPosition *pos = NULL;
194 pos = location_position_new (0, latitude, longitude, 0, LOCATION_STATUS_2D_FIX);
196 __addr_callback_data * calldata = (__addr_callback_data *)malloc(sizeof(__addr_callback_data));
197 if( calldata == NULL)
199 LOGE("[%s] GEOCODER_ERROR_OUT_OF_MEMORY(0x%08x) : fail to create callback data", __FUNCTION__, GEOCODER_ERROR_OUT_OF_MEMORY);
200 return GEOCODER_ERROR_OUT_OF_MEMORY;
202 calldata->callback = callback;
203 calldata->data = user_data;
205 ret = location_map_get_address_from_position_async(handle->object, pos, __cb_address_from_position, calldata);
206 location_position_free(pos);
207 if( ret != LOCATION_ERROR_NONE)
210 return __convert_error_code(ret,(char*)__FUNCTION__);
212 return GEOCODER_ERROR_NONE;
215 int geocoder_foreach_positions_from_address(geocoder_h geocoder,const char* address, geocoder_get_position_cb callback, void *user_data)
217 GEOCODER_NULL_ARG_CHECK(geocoder);
218 GEOCODER_NULL_ARG_CHECK(address);
219 GEOCODER_NULL_ARG_CHECK(callback);
220 geocoder_s *handle = (geocoder_s*)geocoder;
222 char* addr_str = g_strdup(address);
224 __pos_callback_data * calldata = (__pos_callback_data *)malloc(sizeof(__pos_callback_data));
225 if( calldata == NULL)
227 LOGE("[%s] GEOCODER_ERROR_OUT_OF_MEMORY(0x%08x) : fail to create callback data", __FUNCTION__, GEOCODER_ERROR_OUT_OF_MEMORY);
228 return GEOCODER_ERROR_OUT_OF_MEMORY;
230 calldata->callback = callback;
231 calldata->data = user_data;
234 ret = location_map_get_position_from_freeformed_address_async(handle->object, addr_str,__cb_position_from_address, calldata);
236 if( ret != LOCATION_ERROR_NONE)
238 return __convert_error_code(ret,(char*)__FUNCTION__);
240 return GEOCODER_ERROR_NONE;