2 * Copyright (c) 2016 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 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Location
24 /// The LocationBoundary class is an abstract class that provides functions related to the geographic bounds information.
26 /// <since_tizen> 3 </since_tizen>
27 public abstract class LocationBoundary : IDisposable
29 internal IntPtr handle;
30 private bool _disposed = false;
33 /// Gets the location boundary type.
35 /// <since_tizen> 3 </since_tizen>
36 public BoundaryType BoundaryType{ get; internal set; }
38 internal LocationBoundary() { }
40 /// The destructor of the LocationBoundary class.
42 /// <since_tizen> 3 </since_tizen>
45 Log.Info(Globals.LogTag, "The destructor of LocationBoundary class");
49 internal IntPtr GetHandle()
55 /// Checks if the boundary contains the specified geographical coordinates.
57 /// <since_tizen> 3 </since_tizen>
58 /// <param name="coordinate"> The coordinate which needs to be checked.</param>
59 /// <returns>Returns a boolean value indicating whether or not the specified coordinate lies in the geographical area.</returns>
60 public bool BoundaryContainsCoordinates(Coordinate coordinate)
62 Log.Info(Globals.LogTag, "Checking if coordinates are contained within boundary");
63 return Interop.LocationBoundary.IsValidCoordinates(handle, coordinate);
67 /// The overidden Dispose method of the IDisposable class.
69 /// <since_tizen> 3 </since_tizen>
70 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
71 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
74 Log.Info(Globals.LogTag, "Dispose");
76 GC.SuppressFinalize(this);
82 protected virtual void Dispose(bool disposing)
84 Log.Info(Globals.LogTag, "Dispose");
94 private void DestroyHandle()
96 Log.Info(Globals.LogTag, "DestroyBoundaryHandle");
97 int ret = Interop.LocationBoundary.DestroyBoundary(handle);
98 if (((LocationError)ret != LocationError.None))
100 Log.Error(Globals.LogTag, "Error in DestroyBoundary handle" + (LocationError)ret);
101 throw LocationErrorFactory.ThrowLocationException(ret);
107 /// This class represents a rectangular location boundary.
108 /// Inherits the Abstract LocationBoundary class.
110 /// <since_tizen> 3 </since_tizen>
111 public class RectangleBoundary : LocationBoundary
114 /// The constructor of the Rectangle boundary class.
116 /// <since_tizen> 3 </since_tizen>
117 /// <param name="topLeft"> The coordinate which constitutes the top-left handside of the rectangular boundary.</param>
118 /// <param name="bottomRight"> The coordinate which constitutes the bottom-right handside of the rectangular boundary.</param>
119 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
120 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
121 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
122 public RectangleBoundary(Coordinate topLeft, Coordinate bottomRight)
124 Log.Info(Globals.LogTag, "Calling RectangleBoundary constructor");
125 BoundaryType = BoundaryType.Rectangle;
127 int ret = Interop.LocationBoundary.CreateRectangularBoundary(topLeft, bottomRight, out boundsHandle);
128 if ((LocationBoundError)ret != LocationBoundError.None)
130 Log.Error(Globals.LogTag, "Error Creating Rectangular Boundary," + (LocationBoundError)ret);
131 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
133 handle = boundsHandle;
137 /// Gets the top-left handside coordinate of a rectangular boundary.
139 /// <since_tizen> 3 </since_tizen>
140 public Coordinate TopLeft
144 Log.Info(Globals.LogTag, "Calling to get CoordinateItem TopLeft");
145 return GetRectangleCoordinate("TopLeft");
150 /// Gets the bottom-right handside coordinate of a rectangular boundary.
152 /// <since_tizen> 3 </since_tizen>
153 public Coordinate BottomRight
157 Log.Info(Globals.LogTag, "Calling to get CoordinateItem BottomRight");
158 return GetRectangleCoordinate("BottomRight");
162 private Coordinate GetRectangleCoordinate(string tag)
165 Coordinate bottomRight;
167 Interop.LocationBoundary.GetRectangleCoordinates(handle, out topLeft, out bottomRight);
169 if (tag.Equals("TopLeft"))
181 /// This class represents a circular location boundary.
182 /// Inherits the Abstract LocationBoundary class.
184 /// <since_tizen> 3 </since_tizen>
185 public class CircleBoundary : LocationBoundary
188 /// The constructor of the Circular boundary class.
190 /// <since_tizen> 3 </since_tizen>
191 /// <param name="coordinate"> The coordinates which constitute the center of the circular boundary.</param>
192 /// <param name="radius"> The radius value of the circular boundary.</param>
193 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
194 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
195 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
196 public CircleBoundary(Coordinate coordinate, double radius)
198 Log.Info(Globals.LogTag, "Calling CircleBoundary constructor");
199 BoundaryType = BoundaryType.Circle;
201 int ret = Interop.LocationBoundary.CreateCircleBoundary(coordinate, radius, out boundsHandle);
202 if ((LocationBoundError)ret != LocationBoundError.None)
204 Log.Error(Globals.LogTag, "Error Creating Circular Boundary," + (LocationBoundError)ret);
205 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
207 handle = boundsHandle;
211 /// Gets the coordinate of the center of a circular boundary.
213 /// <since_tizen> 3 </since_tizen>
214 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
215 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
216 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
217 public Coordinate Center
221 return GetCircleCenter();
226 /// Gets the radius of a circular boundary.
228 /// <since_tizen> 3 </since_tizen>
229 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
230 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
231 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
240 private Coordinate GetCircleCenter()
242 Log.Info(Globals.LogTag, "Calling to get CoordinateItem Center");
245 int ret = Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
246 if ((LocationBoundError)ret != LocationBoundError.None)
248 Log.Error(Globals.LogTag, "Error Get Circle Center," + (LocationBoundError)ret);
249 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
254 private double GetRadius()
258 int ret = Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
259 if ((LocationBoundError)ret != LocationBoundError.None)
261 Log.Error(Globals.LogTag, "Error Get Radius," + (LocationBoundError)ret);
262 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
269 /// This class represents a polygonal location boundary.
270 /// Inherits the Abstract LocationBoundary class.
272 /// <since_tizen> 3 </since_tizen>
273 public class PolygonBoundary : LocationBoundary
276 /// The constructor of the Polygon Boundary class.
278 /// <since_tizen> 3 </since_tizen>
279 /// <param name="coordinates"> The coordinates which constitute the polgonal boundary.</param>
280 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
281 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
282 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
283 public PolygonBoundary(IList<Coordinate> coordinates)
285 Log.Info(Globals.LogTag, "Calling PolygonBoundary Constructor");
286 if (coordinates == null)
288 Log.Error(Globals.LogTag, "coordingtes list is null");
289 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
292 BoundaryType = BoundaryType.Polygon;
293 IntPtr listPointer = Marshal.AllocHGlobal(Marshal.SizeOf(coordinates[0]) * coordinates.Count);
295 for (int i = 0; i < coordinates.Count; i++)
297 Marshal.StructureToPtr(coordinates[i], listPointer + i * Marshal.SizeOf(coordinates[0]), false);
299 int ret = Interop.LocationBoundary.CreatePolygonBoundary(listPointer, coordinates.Count, out boundsHandle);
300 if ((LocationBoundError)ret != LocationBoundError.None)
302 Log.Error(Globals.LogTag, "Error Creating Polygon Boundary," + (LocationBoundError)ret);
303 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
305 handle = boundsHandle;
309 /// Gets the list of coordinates which constitute a polygonal boundary.
311 /// <since_tizen> 3 </since_tizen>
312 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
313 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
314 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
315 public IList<Coordinate> Coordinates
319 return GetCoordinates();
323 private IList<Coordinate> GetCoordinates()
325 Log.Info(Globals.LogTag, "Calling to get Polygon coordinates");
326 List<Coordinate> coordinateList = new List<Coordinate>();
327 Interop.LocationBoundary.PolygonCoordinatesCallback callback = (Coordinate coordinate, IntPtr userData) =>
330 item.Latitude = coordinate.Latitude;
331 item.Longitude = coordinate.Longitude;
332 coordinateList.Add(item);
336 int ret = Interop.LocationBoundary.GetForEachPolygonCoordinates(handle, callback, IntPtr.Zero);
337 if ((LocationBoundError)ret != LocationBoundError.None)
339 Log.Error(Globals.LogTag, "Error Get foreach Boundary," + (LocationBoundError)ret);
340 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
342 return coordinateList;
347 /// This structure represents the coordinates of a geographical location.
349 /// <since_tizen> 3 </since_tizen>
350 [StructLayout(LayoutKind.Sequential)]
351 public struct Coordinate
354 /// Latitude component of the coordinate.
355 /// Should have a value between [-90.0 ~ 90.0] (degrees).
357 /// <since_tizen> 3 </since_tizen>
358 public double Latitude;
361 /// Longitude component of the coordinate.
362 /// Should have a value between [-180.0 ~ 180.0] (degrees).
364 /// <since_tizen> 3 </since_tizen>
365 public double Longitude;