1 /* Copyright (c) 2010-2014 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.
18 #include "maps_error.h"
19 #include "maps_plugin_types.h"
20 #include "maps_coordinates.h"
21 #include "maps_util.h"
22 #include "maps_condition.h"
24 EXPORT_API int maps_coordinates_create(const double latitude,
25 const double longitude,
26 maps_coordinates_h *coords)
28 if (!maps_condition_check_maps_feature())
29 return MAPS_ERROR_NOT_SUPPORTED;
31 return MAPS_ERROR_INVALID_PARAMETER;
33 MAPS_CHECK_CONDITION(latitude >= -90
34 && latitude <= 90, MAPS_ERROR_INVALID_PARAMETER,
35 "MAPS_ERROR_INVALID_PARAMETER");
36 MAPS_CHECK_CONDITION(longitude >= -180
37 && longitude <= 180, MAPS_ERROR_INVALID_PARAMETER,
38 "MAPS_ERROR_INVALID_PARAMETER");
40 maps_coordinates_s *coord = g_new0(maps_coordinates_s, 1);
43 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
44 return MAPS_ERROR_OUT_OF_MEMORY;
47 coord->latitude = latitude;
48 coord->longitude = longitude;
50 *coords = (maps_coordinates_h) coord;
51 return MAPS_ERROR_NONE;
54 EXPORT_API int maps_coordinates_destroy(maps_coordinates_h coords)
56 if (!maps_condition_check_maps_feature())
57 return MAPS_ERROR_NOT_SUPPORTED;
59 return MAPS_ERROR_INVALID_PARAMETER;
61 maps_coordinates_s *handle = (maps_coordinates_s *) coords;
65 return MAPS_ERROR_NONE;
68 EXPORT_API int maps_coordinates_clone(const maps_coordinates_h origin,
69 maps_coordinates_h *cloned)
71 if (!maps_condition_check_maps_feature())
72 return MAPS_ERROR_NOT_SUPPORTED;
73 if (!cloned || !origin)
74 return MAPS_ERROR_INVALID_PARAMETER;
75 int error = MAPS_ERROR_NONE;
77 maps_coordinates_s *c = (maps_coordinates_s *) origin;
78 error = maps_coordinates_create(c->latitude, c->longitude, cloned);
79 if (!(*cloned) or(error != MAPS_ERROR_NONE))
81 return MAPS_ERROR_NONE;
84 maps_coordinates_destroy(*cloned);
90 /*----------------------------------------------------------------------------*/
92 EXPORT_API int maps_coordinates_get_latitude(const maps_coordinates_h coords,
95 if (!maps_condition_check_maps_feature())
96 return MAPS_ERROR_NOT_SUPPORTED;
97 if (!coords || !latitude)
98 return MAPS_ERROR_INVALID_PARAMETER;
99 *latitude = ((maps_coordinates_s *) coords)->latitude;
100 return MAPS_ERROR_NONE;
103 EXPORT_API int maps_coordinates_get_longitude(const maps_coordinates_h coords,
106 if (!maps_condition_check_maps_feature())
107 return MAPS_ERROR_NOT_SUPPORTED;
108 if (!coords || !longitude)
109 return MAPS_ERROR_INVALID_PARAMETER;
110 *longitude = ((maps_coordinates_s *) coords)->longitude;
111 return MAPS_ERROR_NONE;
114 EXPORT_API int maps_coordinates_get_latitude_longitude(const maps_coordinates_h coords,
118 if (!maps_condition_check_maps_feature())
119 return MAPS_ERROR_NOT_SUPPORTED;
120 if (!coords || !latitude || !longitude)
121 return MAPS_ERROR_INVALID_PARAMETER;
122 *latitude = ((maps_coordinates_s *) coords)->latitude;
123 *longitude = ((maps_coordinates_s *) coords)->longitude;
124 return MAPS_ERROR_NONE;
127 /*----------------------------------------------------------------------------*/
129 EXPORT_API int maps_coordinates_set_latitude(maps_coordinates_h coords,
130 const double latitude)
132 if (!maps_condition_check_maps_feature())
133 return MAPS_ERROR_NOT_SUPPORTED;
135 return MAPS_ERROR_INVALID_PARAMETER;
136 MAPS_CHECK_CONDITION(latitude >= -90 && latitude <= 90,
137 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
138 ((maps_coordinates_s *) coords)->latitude = latitude;
139 return MAPS_ERROR_NONE;
142 EXPORT_API int maps_coordinates_set_longitude(maps_coordinates_h coords,
143 const double longitude)
145 if (!maps_condition_check_maps_feature())
146 return MAPS_ERROR_NOT_SUPPORTED;
148 return MAPS_ERROR_INVALID_PARAMETER;
149 MAPS_CHECK_CONDITION(longitude >= -180 && longitude <= 180,
150 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
151 ((maps_coordinates_s *) coords)->longitude = longitude;
152 return MAPS_ERROR_NONE;
155 EXPORT_API int maps_coordinates_set_latitude_longitude(maps_coordinates_h coords,
156 const double latitude, const double longitude)
158 if (!maps_condition_check_maps_feature())
159 return MAPS_ERROR_NOT_SUPPORTED;
161 return MAPS_ERROR_INVALID_PARAMETER;
162 MAPS_CHECK_CONDITION(latitude >= -90 && latitude <= 90,
163 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
164 MAPS_CHECK_CONDITION(longitude >= -180 && longitude <= 180,
165 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
166 ((maps_coordinates_s *) coords)->latitude = latitude;
167 ((maps_coordinates_s *) coords)->longitude = longitude;
168 return MAPS_ERROR_NONE;
171 int _maps_coordinates_copy(maps_coordinates_h orig, maps_coordinates_h dest)
174 return MAPS_ERROR_INVALID_PARAMETER;
176 ((maps_coordinates_s *)dest)->latitude = ((maps_coordinates_s *)orig)->latitude;
177 ((maps_coordinates_s *)dest)->longitude = ((maps_coordinates_s *)orig)->longitude;
178 return MAPS_ERROR_NONE;
185 EXPORT_API int maps_coordinates_list_create(maps_coordinates_list_h *coordinates_list)
187 if (!maps_condition_check_maps_feature())
188 return MAPS_ERROR_NOT_SUPPORTED;
189 if (!coordinates_list)
190 return MAPS_ERROR_INVALID_PARAMETER;
192 GList *list = g_list_alloc();
193 MAPS_CHECK_CONDITION(list, MAPS_ERROR_OUT_OF_MEMORY, "MAPS_ERROR_OUT_OF_MEMORY");
195 *coordinates_list = (void *) list;
197 return MAPS_ERROR_NONE;
200 static void _free_coordinates(gpointer data)
205 maps_coordinates_s *coordinates = (maps_coordinates_s *) data;
206 ret = maps_coordinates_destroy(coordinates);
208 MAPS_LOGI("Failed to maps_coordinates_destroy!!!"); //LCOV_EXCL_LINE
212 EXPORT_API int maps_coordinates_list_destroy(maps_coordinates_list_h coordinates_list)
214 if (!maps_condition_check_maps_feature())
215 return MAPS_ERROR_NOT_SUPPORTED;
216 if (!coordinates_list)
217 return MAPS_ERROR_INVALID_PARAMETER;
219 GList *list = (GList *) coordinates_list;
221 g_list_free_full(list, (GDestroyNotify) _free_coordinates);
222 coordinates_list = NULL;
224 return MAPS_ERROR_NONE;
227 EXPORT_API int maps_coordinates_list_append(maps_coordinates_list_h coordinates_list, maps_coordinates_h coordinates)
229 if (!maps_condition_check_maps_feature())
230 return MAPS_ERROR_NOT_SUPPORTED;
231 if (!coordinates_list || !coordinates)
232 return MAPS_ERROR_INVALID_PARAMETER;
234 GList *list = (GList *)coordinates_list;
235 maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
236 list = g_list_append(list, coord);
237 coordinates_list = (void *) list;
239 return MAPS_ERROR_NONE;
242 EXPORT_API int maps_coordinates_list_remove(maps_coordinates_list_h coordinates_list, maps_coordinates_h coordinates)
244 if (!maps_condition_check_maps_feature())
245 return MAPS_ERROR_NOT_SUPPORTED;
246 if (!coordinates_list || !coordinates)
247 return MAPS_ERROR_INVALID_PARAMETER;
249 GList *list = (GList *)coordinates_list;
250 maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
251 list = g_list_remove(list, coord);
252 coordinates_list = (void *) list;
254 return MAPS_ERROR_NONE;
257 EXPORT_API int maps_coordinates_list_get_length(maps_coordinates_list_h coordinates_list, int *length)
259 if (!maps_condition_check_maps_feature())
260 return MAPS_ERROR_NOT_SUPPORTED;
261 if (!coordinates_list || !length)
262 return MAPS_ERROR_INVALID_PARAMETER;
264 *length = g_list_length((GList *)coordinates_list) - 1;
266 return MAPS_ERROR_NONE;
269 EXPORT_API int maps_coordinates_list_foreach(maps_coordinates_list_h coordinates_list, maps_coordinates_cb callback, void *user_data)
271 if (!maps_condition_check_maps_feature())
272 return MAPS_ERROR_NOT_SUPPORTED;
273 if (!coordinates_list || !callback)
274 return MAPS_ERROR_INVALID_PARAMETER;
279 GList *list = g_list_first((GList *)coordinates_list);
282 for (l = (GList *)list; l != NULL; l = l->next)
284 maps_coordinates_s *coordinates = (maps_coordinates_s *)l->data;
286 ret = callback(index++, coordinates, user_data);
291 return MAPS_ERROR_NONE;
294 bool maps_coordinates_is_valid(const maps_coordinates_h coordinates)
299 maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
301 MAPS_CHECK_CONDITION(coord->latitude >= -90 && coord->latitude <= 90, MAPS_ERROR_INVALID_PARAMETER,
302 "MAPS_ERROR_INVALID_PARAMETER");
303 MAPS_CHECK_CONDITION(coord->longitude >= -180 && coord->longitude <= 180, MAPS_ERROR_INVALID_PARAMETER,
304 "MAPS_ERROR_INVALID_PARAMETER");