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 /// <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 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);
79 protected virtual void Dispose(bool disposing)
81 Log.Info(Globals.LogTag, "Dispose");
91 private void DestroyHandle()
93 Log.Info(Globals.LogTag, "DestroyBoundaryHandle");
94 int ret = Interop.LocationBoundary.DestroyBoundary(handle);
95 if (((LocationError)ret != LocationError.None))
97 Log.Error(Globals.LogTag, "Error in DestroyBoundary handle" + (LocationError)ret);
98 throw LocationErrorFactory.ThrowLocationException(ret);
104 /// Class representing a rectangular location boundary.
105 /// Inherits the Abstract LocationBoundary class.
107 /// <since_tizen>3</since_tizen>
108 public class RectangleBoundary : LocationBoundary
111 /// Constructor of the Rectangle boundary class.
113 /// <since_tizen>3</since_tizen>
114 /// <param name="topLeft"> The coordinate which constitute the top left handside of the rectangular boundary.</param>
115 /// <param name="bottomRight"> The coordinate which constitute the bottom right handside of the rectangular boundary.</param>
116 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
117 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
118 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
119 public RectangleBoundary(Coordinate topLeft, Coordinate bottomRight)
121 Log.Info(Globals.LogTag, "Calling RectangleBoundary constructor");
122 BoundaryType = BoundaryType.Rectangle;
124 int ret = Interop.LocationBoundary.CreateRectangularBoundary(topLeft, bottomRight, out boundsHandle);
125 if ((LocationBoundError)ret != LocationBoundError.None)
127 Log.Error(Globals.LogTag, "Error Creating Rectangular Boundary," + (LocationBoundError)ret);
128 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
130 handle = boundsHandle;
134 /// Gets the Top Left handside coordinate of a rectangular boundary.
136 /// <since_tizen>3</since_tizen>
137 public Coordinate TopLeft
141 Log.Info(Globals.LogTag, "Calling to get CoordinateItem TopLeft");
142 return GetRectangleCoordinate("TopLeft");
147 /// Gets the Bottom Right handside coordinate of a rectangular boundary.
149 /// <since_tizen>3</since_tizen>
150 public Coordinate BottomRight
154 Log.Info(Globals.LogTag, "Calling to get CoordinateItem BottomRight");
155 return GetRectangleCoordinate("BottomRight");
159 private Coordinate GetRectangleCoordinate(string tag)
162 Coordinate bottomRight;
164 Interop.LocationBoundary.GetRectangleCoordinates(handle, out topLeft, out bottomRight);
166 if (tag.Equals("TopLeft"))
178 /// Class representing a circular location boundary.
179 /// Inherits the Abstract LocationBoundary class.
181 /// <since_tizen>3</since_tizen>
182 public class CircleBoundary : LocationBoundary
185 /// Constructor of the Circular boundary class.
187 /// <since_tizen>3</since_tizen>
188 /// <param name="coordinate"> The coordinates which constitute the center of the circular boundary.</param>
189 /// <param name="radius"> The radius value of the circular boundary.</param>
190 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
191 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
192 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
193 public CircleBoundary(Coordinate coordinate, double radius)
195 Log.Info(Globals.LogTag, "Calling CircleBoundary constructor");
196 BoundaryType = BoundaryType.Circle;
198 int ret = Interop.LocationBoundary.CreateCircleBoundary(coordinate, radius, out boundsHandle);
199 if ((LocationBoundError)ret != LocationBoundError.None)
201 Log.Error(Globals.LogTag, "Error Creating Circular Boundary," + (LocationBoundError)ret);
202 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
204 handle = boundsHandle;
208 /// Gets the coordinate of the center of a circular boundary.
210 /// <since_tizen>3</since_tizen>
211 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
212 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
213 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
214 public Coordinate Center
218 return GetCircleCenter();
223 /// Gets the radius of a circular boundary.
225 /// <since_tizen>3</since_tizen>
226 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
227 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
228 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
237 private Coordinate GetCircleCenter()
239 Log.Info(Globals.LogTag, "Calling to get CoordinateItem Center");
242 int ret = Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
243 if ((LocationBoundError)ret != LocationBoundError.None)
245 Log.Error(Globals.LogTag, "Error Get Circle Center," + (LocationBoundError)ret);
246 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
251 private double GetRadius()
255 int ret = Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
256 if ((LocationBoundError)ret != LocationBoundError.None)
258 Log.Error(Globals.LogTag, "Error Get Radius," + (LocationBoundError)ret);
259 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
266 /// Class representing a polygonal location boundary.
267 /// Inherits the Abstract LocationBoundary class.
269 /// <since_tizen>3</since_tizen>
270 public class PolygonBoundary : LocationBoundary
273 /// Constructor of the polygon boundary class.
275 /// <since_tizen>3</since_tizen>
276 /// <param name="coordinates"> The coordinates which constitute the polgonal boundary.</param>
277 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
278 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
279 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
280 public PolygonBoundary(IList<Coordinate> coordinates)
282 Log.Info(Globals.LogTag, "Calling PolygonBoundary Constructor");
283 if (coordinates == null)
285 Log.Error(Globals.LogTag, "coordingtes list is null");
286 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
289 BoundaryType = BoundaryType.Polygon;
290 IntPtr listPointer = Marshal.AllocHGlobal(Marshal.SizeOf(coordinates[0]) * coordinates.Count);
292 for (int i = 0; i < coordinates.Count; i++)
294 Marshal.StructureToPtr(coordinates[i], listPointer + i * Marshal.SizeOf(coordinates[0]), false);
296 int ret = Interop.LocationBoundary.CreatePolygonBoundary(listPointer, coordinates.Count, out boundsHandle);
297 if ((LocationBoundError)ret != LocationBoundError.None)
299 Log.Error(Globals.LogTag, "Error Creating Polygon Boundary," + (LocationBoundError)ret);
300 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
302 handle = boundsHandle;
306 /// Gets the list of coordinates which constitute a polygonal boundary
308 /// <since_tizen>3</since_tizen>
309 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
310 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
311 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
312 public IList<Coordinate> Coordinates
316 return GetCoordinates();
320 private IList<Coordinate> GetCoordinates()
322 Log.Info(Globals.LogTag, "Calling to get Polygon coordinates");
323 List<Coordinate> coordinateList = new List<Coordinate>();
324 Interop.LocationBoundary.PolygonCoordinatesCallback callback = (Coordinate coordinate, IntPtr userData) =>
327 item.Latitude = coordinate.Latitude;
328 item.Longitude = coordinate.Longitude;
329 coordinateList.Add(item);
333 int ret = Interop.LocationBoundary.GetForEachPolygonCoordinates(handle, callback, IntPtr.Zero);
334 if ((LocationBoundError)ret != LocationBoundError.None)
336 Log.Error(Globals.LogTag, "Error Get foreach Boundary," + (LocationBoundError)ret);
337 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
339 return coordinateList;
344 /// The structure which represents the co-ordinates of a geographical location.
346 /// <since_tizen>3</since_tizen>
347 [StructLayout(LayoutKind.Sequential)]
348 public struct Coordinate
351 /// Latitude component of the co-ordinate.
352 /// Should have a value between [-90.0 ~ 90.0] (degrees).
354 /// <since_tizen>3</since_tizen>
355 public double Latitude;
358 /// Longitude component of the co-ordinate.
359 /// Should have a value between [-180.0 ~ 180.0] (degrees).
361 /// <since_tizen>3</since_tizen>
362 public double Longitude;