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);
42 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
43 return MAPS_ERROR_OUT_OF_MEMORY;
45 coord->latitude = latitude;
46 coord->longitude = longitude;
48 *coords = (maps_coordinates_h) coord;
49 return MAPS_ERROR_NONE;
52 EXPORT_API int maps_coordinates_destroy(maps_coordinates_h coords)
54 if (!maps_condition_check_maps_feature())
55 return MAPS_ERROR_NOT_SUPPORTED;
57 return MAPS_ERROR_INVALID_PARAMETER;
59 maps_coordinates_s *handle = (maps_coordinates_s *) coords;
63 return MAPS_ERROR_NONE;
66 EXPORT_API int maps_coordinates_clone(const maps_coordinates_h origin,
67 maps_coordinates_h *cloned)
69 if (!maps_condition_check_maps_feature())
70 return MAPS_ERROR_NOT_SUPPORTED;
71 if (!cloned || !origin)
72 return MAPS_ERROR_INVALID_PARAMETER;
73 int error = MAPS_ERROR_NONE;
75 maps_coordinates_s *c = (maps_coordinates_s *) origin;
76 error = maps_coordinates_create(c->latitude, c->longitude, cloned);
77 if (!(*cloned) or(error != MAPS_ERROR_NONE))
79 return MAPS_ERROR_NONE;
81 maps_coordinates_destroy(*cloned);
86 /*----------------------------------------------------------------------------*/
88 EXPORT_API int maps_coordinates_get_latitude(const maps_coordinates_h coords,
91 if (!maps_condition_check_maps_feature())
92 return MAPS_ERROR_NOT_SUPPORTED;
93 if (!coords || !latitude)
94 return MAPS_ERROR_INVALID_PARAMETER;
95 *latitude = ((maps_coordinates_s *) coords)->latitude;
96 return MAPS_ERROR_NONE;
99 EXPORT_API int maps_coordinates_get_longitude(const maps_coordinates_h coords,
102 if (!maps_condition_check_maps_feature())
103 return MAPS_ERROR_NOT_SUPPORTED;
104 if (!coords || !longitude)
105 return MAPS_ERROR_INVALID_PARAMETER;
106 *longitude = ((maps_coordinates_s *) coords)->longitude;
107 return MAPS_ERROR_NONE;
110 EXPORT_API int maps_coordinates_get_latitude_longitude(const maps_coordinates_h coords,
114 if (!maps_condition_check_maps_feature())
115 return MAPS_ERROR_NOT_SUPPORTED;
116 if (!coords || !latitude || !longitude)
117 return MAPS_ERROR_INVALID_PARAMETER;
118 *latitude = ((maps_coordinates_s *) coords)->latitude;
119 *longitude = ((maps_coordinates_s *) coords)->longitude;
120 return MAPS_ERROR_NONE;
123 /*----------------------------------------------------------------------------*/
125 EXPORT_API int maps_coordinates_set_latitude(maps_coordinates_h coords,
126 const double latitude)
128 if (!maps_condition_check_maps_feature())
129 return MAPS_ERROR_NOT_SUPPORTED;
131 return MAPS_ERROR_INVALID_PARAMETER;
132 MAPS_CHECK_CONDITION(latitude >= -90 && latitude <= 90,
133 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
134 ((maps_coordinates_s *) coords)->latitude = latitude;
135 return MAPS_ERROR_NONE;
138 EXPORT_API int maps_coordinates_set_longitude(maps_coordinates_h coords,
139 const double longitude)
141 if (!maps_condition_check_maps_feature())
142 return MAPS_ERROR_NOT_SUPPORTED;
144 return MAPS_ERROR_INVALID_PARAMETER;
145 MAPS_CHECK_CONDITION(longitude >= -180 && longitude <= 180,
146 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
147 ((maps_coordinates_s *) coords)->longitude = longitude;
148 return MAPS_ERROR_NONE;
151 EXPORT_API int maps_coordinates_set_latitude_longitude(maps_coordinates_h coords,
152 const double latitude, const double longitude)
154 if (!maps_condition_check_maps_feature())
155 return MAPS_ERROR_NOT_SUPPORTED;
157 return MAPS_ERROR_INVALID_PARAMETER;
158 MAPS_CHECK_CONDITION(latitude >= -90 && latitude <= 90,
159 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
160 MAPS_CHECK_CONDITION(longitude >= -180 && longitude <= 180,
161 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
162 ((maps_coordinates_s *) coords)->latitude = latitude;
163 ((maps_coordinates_s *) coords)->longitude = longitude;
164 return MAPS_ERROR_NONE;
167 int _maps_coordinates_copy(maps_coordinates_h orig, maps_coordinates_h dest)
170 return MAPS_ERROR_INVALID_PARAMETER;
172 ((maps_coordinates_s *)dest)->latitude = ((maps_coordinates_s *)orig)->latitude;
173 ((maps_coordinates_s *)dest)->longitude = ((maps_coordinates_s *)orig)->longitude;
174 return MAPS_ERROR_NONE;
181 EXPORT_API int maps_coordinates_list_create(maps_coordinates_list_h *coordinates_list)
183 if (!maps_condition_check_maps_feature())
184 return MAPS_ERROR_NOT_SUPPORTED;
185 if (!coordinates_list)
186 return MAPS_ERROR_INVALID_PARAMETER;
188 GList *list = g_list_alloc();
189 MAPS_CHECK_CONDITION(list, MAPS_ERROR_OUT_OF_MEMORY, "MAPS_ERROR_OUT_OF_MEMORY");
191 *coordinates_list = (void *) list;
193 return MAPS_ERROR_NONE;
196 static void _free_coordinates(gpointer data)
201 maps_coordinates_s *coordinates = (maps_coordinates_s *) data;
202 ret = maps_coordinates_destroy(coordinates);
204 MAPS_LOGI("Failed to maps_coordinates_destroy!!!");
208 EXPORT_API int maps_coordinates_list_destroy(maps_coordinates_list_h coordinates_list)
210 if (!maps_condition_check_maps_feature())
211 return MAPS_ERROR_NOT_SUPPORTED;
212 if (!coordinates_list)
213 return MAPS_ERROR_INVALID_PARAMETER;
215 GList *list = (GList *) coordinates_list;
217 g_list_free_full(list, (GDestroyNotify) _free_coordinates);
218 coordinates_list = NULL;
220 return MAPS_ERROR_NONE;
223 EXPORT_API int maps_coordinates_list_append(maps_coordinates_list_h coordinates_list, maps_coordinates_h coordinates)
225 if (!maps_condition_check_maps_feature())
226 return MAPS_ERROR_NOT_SUPPORTED;
227 if (!coordinates_list || !coordinates)
228 return MAPS_ERROR_INVALID_PARAMETER;
230 GList *list = (GList *)coordinates_list;
231 maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
232 list = g_list_append(list, coord);
233 coordinates_list = (void *) list;
235 return MAPS_ERROR_NONE;
238 EXPORT_API int maps_coordinates_list_remove(maps_coordinates_list_h coordinates_list, maps_coordinates_h coordinates)
240 if (!maps_condition_check_maps_feature())
241 return MAPS_ERROR_NOT_SUPPORTED;
242 if (!coordinates_list || !coordinates)
243 return MAPS_ERROR_INVALID_PARAMETER;
245 GList *list = (GList *)coordinates_list;
246 maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
247 list = g_list_remove(list, coord);
248 coordinates_list = (void *) list;
250 return MAPS_ERROR_NONE;
253 EXPORT_API int maps_coordinates_list_get_length(maps_coordinates_list_h coordinates_list, int *length)
255 if (!maps_condition_check_maps_feature())
256 return MAPS_ERROR_NOT_SUPPORTED;
257 if (!coordinates_list || !length)
258 return MAPS_ERROR_INVALID_PARAMETER;
260 *length = g_list_length((GList *)coordinates_list) - 1;
262 return MAPS_ERROR_NONE;
265 EXPORT_API int maps_coordinates_list_foreach(maps_coordinates_list_h coordinates_list, maps_coordinates_cb callback, void *user_data)
267 if (!maps_condition_check_maps_feature())
268 return MAPS_ERROR_NOT_SUPPORTED;
269 if (!coordinates_list || !callback)
270 return MAPS_ERROR_INVALID_PARAMETER;
275 GList *list = g_list_first((GList *)coordinates_list);
278 for (l = (GList *)list; l != NULL; l = l->next)
280 maps_coordinates_s *coordinates = (maps_coordinates_s *)l->data;
282 ret = callback(index++, coordinates, user_data);
287 return MAPS_ERROR_NONE;
290 bool maps_coordinates_is_valid(const maps_coordinates_h coordinates)
295 maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
297 MAPS_CHECK_CONDITION(coord->latitude >= -90 && coord->latitude <= 90, MAPS_ERROR_INVALID_PARAMETER,
298 "MAPS_ERROR_INVALID_PARAMETER");
299 MAPS_CHECK_CONDITION(coord->longitude >= -180 && coord->longitude <= 180, MAPS_ERROR_INVALID_PARAMETER,
300 "MAPS_ERROR_INVALID_PARAMETER");