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 /// Abstract class which provides functions related to geographic bounds information.
26 public abstract class LocationBoundary : IDisposable
28 internal IntPtr handle;
29 private bool _disposed = false;
32 /// Gets the location boundary type.
34 public BoundaryType BoundaryType{ get; internal set; }
36 internal LocationBoundary() { }
38 /// The destructor of LocationBoundary class.
42 Log.Info(Globals.LogTag, "The destructor of LocationBoundary class");
46 internal IntPtr GetHandle()
52 /// Checks if the boundary contains the specified geographical coordinates.
54 /// <param name="coordinate"> The coordinate which needs to be checked.</param>
55 /// <returns>Returns a boolean value indicating whether or not the specified coordinate lies in the geographical area.</returns>
56 public bool BoundaryContainsCoordinates(Coordinate coordinate)
58 Log.Info(Globals.LogTag, "Checking if coordinates are contained within boundary");
59 return Interop.LocationBoundary.IsValidCoordinates(handle, coordinate);
63 /// The overidden Dispose method of the IDisposable class.
65 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
66 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
69 Log.Info(Globals.LogTag, "Dispose");
71 GC.SuppressFinalize(this);
74 protected virtual void Dispose(bool disposing)
76 Log.Info(Globals.LogTag, "Dispose");
86 private void DestroyHandle()
88 Log.Info(Globals.LogTag, "DestroyBoundaryHandle");
89 int ret = Interop.LocationBoundary.DestroyBoundary(handle);
90 if (((LocationError)ret != LocationError.None))
92 Log.Error(Globals.LogTag, "Error in DestroyBoundary handle" + (LocationError)ret);
93 throw LocationErrorFactory.ThrowLocationException(ret);
99 /// Class representing a rectangular location boundary.
100 /// Inherits the Abstract LocationBoundary class.
102 public class RectangleBoundary : LocationBoundary
105 /// Constructor of the Rectangle boundary class.
107 /// <param name="topLeft"> The coordinate which constitute the top left handside of the rectangular boundary.</param>
108 /// <param name="bottomRight"> The coordinate which constitute the bottom right handside of the rectangular boundary.</param>
109 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
110 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
111 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
112 public RectangleBoundary(Coordinate topLeft, Coordinate bottomRight)
114 Log.Info(Globals.LogTag, "Calling RectangleBoundary constructor");
115 BoundaryType = BoundaryType.Rectangle;
117 int ret = Interop.LocationBoundary.CreateRectangularBoundary(topLeft, bottomRight, out boundsHandle);
118 if ((LocationBoundError)ret != LocationBoundError.None)
120 Log.Error(Globals.LogTag, "Error Creating Rectangular Boundary," + (LocationBoundError)ret);
121 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
123 handle = boundsHandle;
127 /// Gets the Top Left handside coordinate of a rectangular boundary.
129 public Coordinate TopLeft
133 Log.Info(Globals.LogTag, "Calling to get CoordinateItem TopLeft");
134 return GetRectangleCoordinate("TopLeft");
139 /// Gets the Bottom Right handside coordinate of a rectangular boundary.
141 public Coordinate BottomRight
145 Log.Info(Globals.LogTag, "Calling to get CoordinateItem BottomRight");
146 return GetRectangleCoordinate("BottomRight");
150 private Coordinate GetRectangleCoordinate(string tag)
153 Coordinate bottomRight;
155 Interop.LocationBoundary.GetRectangleCoordinates(handle, out topLeft, out bottomRight);
157 if (tag.Equals("TopLeft"))
169 /// Class representing a circular location boundary.
170 /// Inherits the Abstract LocationBoundary class.
172 public class CircleBoundary : LocationBoundary
175 /// Constructor of the Circular boundary class.
177 /// <param name="coordinate"> The coordinates which constitute the center of the circular boundary.</param>
178 /// <param name="radius"> The radius value of the circular boundary.</param>
179 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
180 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
181 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
182 public CircleBoundary(Coordinate coordinate, double radius)
184 Log.Info(Globals.LogTag, "Calling CircleBoundary constructor");
185 BoundaryType = BoundaryType.Circle;
187 int ret = Interop.LocationBoundary.CreateCircleBoundary(coordinate, radius, out boundsHandle);
188 if ((LocationBoundError)ret != LocationBoundError.None)
190 Log.Error(Globals.LogTag, "Error Creating Circular Boundary," + (LocationBoundError)ret);
191 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
193 handle = boundsHandle;
197 /// Gets the coordinate of the center of a circular boundary.
199 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
200 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
201 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
202 public Coordinate Center
206 return GetCircleCenter();
211 /// Gets the radius of a circular boundary.
213 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
214 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
215 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
224 private Coordinate GetCircleCenter()
226 Log.Info(Globals.LogTag, "Calling to get CoordinateItem Center");
229 int ret = Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
230 if ((LocationBoundError)ret != LocationBoundError.None)
232 Log.Error(Globals.LogTag, "Error Get Circle Center," + (LocationBoundError)ret);
233 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
238 private double GetRadius()
242 int ret = Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
243 if ((LocationBoundError)ret != LocationBoundError.None)
245 Log.Error(Globals.LogTag, "Error Get Radius," + (LocationBoundError)ret);
246 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
253 /// Class representing a polygonal location boundary.
254 /// Inherits the Abstract LocationBoundary class.
256 public class PolygonBoundary : LocationBoundary
259 /// Constructor of the polygon boundary class.
261 /// <param name="coordinates"> The coordinates which constitute the polgonal boundary.</param>
262 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
263 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
264 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
265 public PolygonBoundary(IList<Coordinate> coordinates)
267 Log.Info(Globals.LogTag, "Calling PolygonBoundary Constructor");
268 if (coordinates == null)
270 Log.Error(Globals.LogTag, "coordingtes list is null");
271 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
274 BoundaryType = BoundaryType.Polygon;
275 IntPtr listPointer = Marshal.AllocHGlobal(Marshal.SizeOf(coordinates[0]) * coordinates.Count);
277 for (int i = 0; i < coordinates.Count; i++)
279 Marshal.StructureToPtr(coordinates[i], listPointer + i * Marshal.SizeOf(coordinates[0]), false);
281 int ret = Interop.LocationBoundary.CreatePolygonBoundary(listPointer, coordinates.Count, out boundsHandle);
282 if ((LocationBoundError)ret != LocationBoundError.None)
284 Log.Error(Globals.LogTag, "Error Creating Polygon Boundary," + (LocationBoundError)ret);
285 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
287 handle = boundsHandle;
291 /// Gets the list of coordinates which constitute a polygonal boundary
293 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
294 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
295 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
296 public IList<Coordinate> Coordinates
300 return GetCoordinates();
304 private IList<Coordinate> GetCoordinates()
306 Log.Info(Globals.LogTag, "Calling to get Polygon coordinates");
307 List<Coordinate> coordinateList = new List<Coordinate>();
308 Interop.LocationBoundary.PolygonCoordinatesCallback callback = (Coordinate coordinate, IntPtr userData) =>
311 item.Latitude = coordinate.Latitude;
312 item.Longitude = coordinate.Longitude;
313 coordinateList.Add(item);
317 int ret = Interop.LocationBoundary.GetForEachPolygonCoordinates(handle, callback, IntPtr.Zero);
318 if ((LocationBoundError)ret != LocationBoundError.None)
320 Log.Error(Globals.LogTag, "Error Get foreach Boundary," + (LocationBoundError)ret);
321 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
323 return coordinateList;
328 /// The structure which represents the co-ordinates of a geographical location.
330 [StructLayout(LayoutKind.Sequential)]
331 public struct Coordinate
334 /// Latitude component of the co-ordinate.
335 /// Should have a value between [-90.0 ~ 90.0] (degrees).
337 public double Latitude;
340 /// Longitude component of the co-ordinate.
341 /// Should have a value between [-180.0 ~ 180.0] (degrees).
343 public double Longitude;