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; }
37 /// The destructor of LocationBoundary class.
41 Log.Info(Globals.LogTag, "The destructor of LocationBoundary class");
45 internal IntPtr GetHandle()
51 /// Checks if the boundary contains the specified geographical coordinates.
53 /// <param name="coordinate"> The coordinate which needs to be checked.</param>
54 /// <returns>Returns a boolean value indicating whether or not the specified coordinate lies in the geographical area.</returns>
55 public bool BoundaryContainsCoordinates(Coordinate coordinate)
57 Log.Info(Globals.LogTag, "Checking if coordinates are contained within boundary");
58 return Interop.LocationBoundary.IsValidCoordinates(handle, coordinate);
62 /// The overidden Dispose method of the IDisposable class.
64 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
65 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
68 Log.Info(Globals.LogTag, "Dispose");
70 GC.SuppressFinalize(this);
73 protected virtual void Dispose(bool disposing)
75 Log.Info(Globals.LogTag, "Dispose");
85 private void DestroyHandle()
87 Log.Info(Globals.LogTag, "DestroyBoundaryHandle");
88 int ret = Interop.LocationBoundary.DestroyBoundary(handle);
89 if (((LocationError)ret != LocationError.None))
91 Log.Error(Globals.LogTag, "Error in DestroyBoundary handle" + (LocationError)ret);
92 throw LocationErrorFactory.ThrowLocationException(ret);
98 /// Class representing a rectangular location boundary.
99 /// Inherits the Abstract LocationBoundary class.
101 public class RectangleBoundary : LocationBoundary
104 /// Constructor of the Rectangle boundary class.
106 /// <param name="topLeft"> The coordinate which constitute the top left handside of the rectangular boundary.</param>
107 /// <param name="bottomRight"> The coordinate which constitute the bottom right handside of the rectangular boundary.</param>
108 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
109 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
110 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
111 public RectangleBoundary(Coordinate topLeft, Coordinate bottomRight)
113 Log.Info(Globals.LogTag, "Calling RectangleBoundary constructor");
114 BoundaryType = BoundaryType.Rectangle;
116 int ret = Interop.LocationBoundary.CreateRectangularBoundary(topLeft, bottomRight, out boundsHandle);
117 if ((LocationBoundError)ret != LocationBoundError.None)
119 Log.Error(Globals.LogTag, "Error Creating Rectangular Boundary," + (LocationBoundError)ret);
120 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
122 handle = boundsHandle;
126 /// Gets the Top Left handside coordinate of a rectangular boundary.
128 public Coordinate TopLeft
132 Log.Info(Globals.LogTag, "Calling to get CoordinateItem TopLeft");
133 return GetRectangleCoordinate("TopLeft");
138 /// Gets the Bottom Right handside coordinate of a rectangular boundary.
140 public Coordinate BottomRight
144 Log.Info(Globals.LogTag, "Calling to get CoordinateItem BottomRight");
145 return GetRectangleCoordinate("BottomRight");
149 private Coordinate GetRectangleCoordinate(string tag)
152 Coordinate bottomRight;
154 Interop.LocationBoundary.GetRectangleCoordinates(handle, out topLeft, out bottomRight);
156 if (tag.Equals("TopLeft"))
168 /// Class representing a circular location boundary.
169 /// Inherits the Abstract LocationBoundary class.
171 public class CircleBoundary : LocationBoundary
174 /// Constructor of the Circular boundary class.
176 /// <param name="coordinate"> The coordinates which constitute the center of the circular boundary.</param>
177 /// <param name="radius"> The radius value of the circular boundary.</param>
178 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
179 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
180 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
181 public CircleBoundary(Coordinate coordinate, double radius)
183 Log.Info(Globals.LogTag, "Calling CircleBoundary constructor");
184 BoundaryType = BoundaryType.Circle;
186 int ret = Interop.LocationBoundary.CreateCircleBoundary(coordinate, radius, out boundsHandle);
187 if ((LocationBoundError)ret != LocationBoundError.None)
189 Log.Error(Globals.LogTag, "Error Creating Circular Boundary," + (LocationBoundError)ret);
190 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
192 handle = boundsHandle;
196 /// Gets the coordinate of the center of a circular boundary.
198 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
199 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
200 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
201 public Coordinate Center
205 return GetCircleCenter();
210 /// Gets the radius of a circular boundary.
212 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
213 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
214 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
223 private Coordinate GetCircleCenter()
225 Log.Info(Globals.LogTag, "Calling to get CoordinateItem Center");
228 int ret = Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
229 if ((LocationBoundError)ret != LocationBoundError.None)
231 Log.Error(Globals.LogTag, "Error Get Circle Center," + (LocationBoundError)ret);
232 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
237 private double GetRadius()
241 int ret = Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
242 if ((LocationBoundError)ret != LocationBoundError.None)
244 Log.Error(Globals.LogTag, "Error Get Radius," + (LocationBoundError)ret);
245 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
252 /// Class representing a polygonal location boundary.
253 /// Inherits the Abstract LocationBoundary class.
255 public class PolygonBoundary : LocationBoundary
258 /// Constructor of the polygon boundary class.
260 /// <param name="coordinates"> The coordinates which constitute the polgonal boundary.</param>
261 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
262 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
263 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
264 public PolygonBoundary(IList<Coordinate> coordinates)
266 Log.Info(Globals.LogTag, "Calling PolygonBoundary Constructor");
267 BoundaryType = BoundaryType.Polygon;
268 IntPtr listPointer = Marshal.AllocHGlobal(Marshal.SizeOf(coordinates[0]) * coordinates.Count);
270 for (int i = 0; i < coordinates.Count; i++)
272 Marshal.StructureToPtr(coordinates[i], listPointer + i * Marshal.SizeOf(coordinates[0]), false);
274 int ret = Interop.LocationBoundary.CreatePolygonBoundary(listPointer, coordinates.Count, out boundsHandle);
275 if ((LocationBoundError)ret != LocationBoundError.None)
277 Log.Error(Globals.LogTag, "Error Creating Polygon Boundary," + (LocationBoundError)ret);
278 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
280 handle = boundsHandle;
284 /// Gets the list of coordinates which constitute a polygonal boundary
286 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
287 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
288 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
289 public IList<Coordinate> Coordinates
293 return GetCoordinates();
297 private IList<Coordinate> GetCoordinates()
299 Log.Info(Globals.LogTag, "Calling to get Polygon coordinates");
300 List<Coordinate> coordinateList = new List<Coordinate>();
301 Interop.LocationBoundary.PolygonCoordinatesCallback callback = (Coordinate coordinate, IntPtr userData) =>
304 item.Latitude = coordinate.Latitude;
305 item.Longitude = coordinate.Longitude;
306 coordinateList.Add(item);
310 int ret = Interop.LocationBoundary.GetForEachPolygonCoordinates(handle, callback, IntPtr.Zero);
311 if ((LocationBoundError)ret != LocationBoundError.None)
313 Log.Error(Globals.LogTag, "Error Get foreach Boundary," + (LocationBoundError)ret);
314 throw LocationErrorFactory.ThrowLocationBoundaryException(ret);
316 return coordinateList;
321 /// The structure which represents the co-ordinates of a geographical location.
323 [StructLayout(LayoutKind.Sequential)]
324 public struct Coordinate
327 /// Latitude component of the co-ordinate.
328 /// Should have a value between [-90.0 ~ 90.0] (degrees).
330 public double Latitude;
333 /// Longitude component of the co-ordinate.
334 /// Should have a value between [-180.0 ~ 180.0] (degrees).
336 public double Longitude;