1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
10 using System.Collections.Generic;
11 using System.Runtime.InteropServices;
13 namespace Tizen.Location
16 /// Abstract class which provides functions related to geographic bounds information.
18 public abstract class LocationBoundary
20 internal IntPtr handle;
23 /// Gets the location boundary type.
25 public BoundaryType BoundaryType{ get; internal set; }
27 internal IntPtr GetHandle()
33 /// Checks if the boundary contains the specified geographical coordinates.
35 /// <param name="coordinate"> The coordinate which needs to be checked.</param>
36 /// <returns>Returns a boolean value indicating whether or not the specified coordinate lies in the geographical area.</returns>
37 public bool BoundaryContainsCoordinates(Coordinate coordinate)
39 Log.Info(Globals.LogTag, "Checking if coordinates are contained within boundary");
40 return Interop.LocationBoundary.IsValidCoordinates(handle, coordinate);
45 /// Class representing a rectangular location boundary.
46 /// Inherits the Abstract LocationBoundary class.
48 public class RectangleBoundary : LocationBoundary
51 /// Constructor of the Rectangle boundary class.
53 /// <param name="topLeft"> The coordinate which constitute the top left handside of the rectangular boundary.</param>
54 /// <param name="bottomRight"> The coordinate which constitute the bottom right handside of the rectangular boundary.</param>
55 public RectangleBoundary(Coordinate topLeft, Coordinate bottomRight)
57 Log.Info(Globals.LogTag, "Calling RectangleBoundary constructor");
58 BoundaryType = BoundaryType.Rectangle;
60 int ret = Interop.LocationBoundary.CreateRectangularBoundary(topLeft, bottomRight, out boundsHandle);
61 if ((LocationBoundError)ret != LocationBoundError.None)
63 Log.Error(Globals.LogTag, "Error Creating Rectangular Boundary," + (LocationBoundError)ret);
64 LocationErrorFactory.ThrowLocationBoundaryException(ret);
66 handle = boundsHandle;
70 /// Gets the Top Left handside coordinate of a rectangular boundary.
72 public Coordinate TopLeft
76 Log.Info(Globals.LogTag, "Calling to get CoordinateItem TopLeft");
77 return GetRectangleCoordinate("TopLeft");
82 /// Gets the Bottom Right handside coordinate of a rectangular boundary.
84 public Coordinate BottomRight
88 Log.Info(Globals.LogTag, "Calling to get CoordinateItem BottomRight");
89 return GetRectangleCoordinate("BottomRight");
93 private Coordinate GetRectangleCoordinate(string tag)
96 Coordinate bottomRight;
98 Interop.LocationBoundary.GetRectangleCoordinates(handle, out topLeft, out bottomRight);
100 if (tag.Equals("TopLeft"))
112 /// Class representing a circular location boundary.
113 /// Inherits the Abstract LocationBoundary class.
115 public class CircleBoundary : LocationBoundary
118 /// Constructor of the Circular boundary class.
120 /// <param name="coordinate"> The coordinates which constitute the center of the circular boundary.</param>
121 /// <param name="radius"> The radius value of the circular boundary.</param>
122 public CircleBoundary(Coordinate coordinate, double radius)
124 Log.Info(Globals.LogTag, "Calling CircleBoundary constructor");
125 BoundaryType = BoundaryType.Circle;
127 int ret = Interop.LocationBoundary.CreateCircleBoundary(coordinate, radius, out boundsHandle);
128 if ((LocationBoundError)ret != LocationBoundError.None)
130 Log.Error(Globals.LogTag, "Error Creating Circular Boundary," + (LocationBoundError)ret);
131 LocationErrorFactory.ThrowLocationBoundaryException(ret);
133 handle = boundsHandle;
137 /// Gets the coordinate of the center of a circular boundary.
139 public Coordinate Center
143 return GetCircleCenter();
148 /// Gets the radius of a circular boundary.
158 private Coordinate GetCircleCenter()
160 Log.Info(Globals.LogTag, "Calling to get CoordinateItem Center");
163 Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
167 private double GetRadius()
171 Interop.LocationBoundary.GetCircleCoordinates(handle, out center, out radius);
177 /// Class representing a polygonal location boundary.
178 /// Inherits the Abstract LocationBoundary class.
180 public class PolygonBoundary : LocationBoundary
183 /// Constructor of the polygon boundary class.
185 /// <param name="coordinates"> The coordinates which constitute the polgonal boundary.</param>
186 public PolygonBoundary(IList<Coordinate> coordinates)
188 Log.Info(Globals.LogTag, "Calling PolygonBoundary Constructor");
189 BoundaryType = BoundaryType.Polygon;
190 IntPtr[] pointers = new IntPtr[coordinates.Count];
191 IntPtr listPointer = Marshal.AllocHGlobal(Marshal.SizeOf(coordinates[0]) * coordinates.Count);
193 for (int i = 0; i < coordinates.Count; i++)
195 pointers[i] = Marshal.AllocHGlobal(Marshal.SizeOf(coordinates[0]));
196 Marshal.StructureToPtr(coordinates[i], pointers[i], true);
197 Marshal.WriteIntPtr(listPointer, i * Marshal.SizeOf(coordinates[0]), pointers[i]);
199 int ret = Interop.LocationBoundary.CreatePolygonBoundary(listPointer, coordinates.Count, out boundsHandle);
200 if ((LocationBoundError)ret != LocationBoundError.None)
202 Log.Error(Globals.LogTag, "Error Creating Polygon Boundary," + (LocationBoundError)ret);
203 LocationErrorFactory.ThrowLocationBoundaryException(ret);
205 handle = boundsHandle;
209 /// Gets the list of coordinates which constitute a polygonal boundary
211 public IList<Coordinate> Coordinates
215 return GetCoordinates();
219 private IList<Coordinate> GetCoordinates()
221 Log.Info(Globals.LogTag, "Calling to get Polygon coordinates");
222 List<Coordinate> coordinateList = new List<Coordinate>();
223 Interop.LocationBoundary.PolygonCoordinatesCallback callback = (Coordinate coordinate, IntPtr userData) =>
226 item.Latitude = coordinate.Latitude;
227 item.Longitude = coordinate.Longitude;
228 coordinateList.Add(item);
232 Interop.LocationBoundary.GetForEachPolygonCoordinates(handle, callback, IntPtr.Zero);
233 return coordinateList;
238 /// The structure which represents the co-ordinates of a geographical location.
240 [StructLayout(LayoutKind.Sequential)]
241 public struct Coordinate
244 /// Latitude component of the co-ordinate.
245 /// Should have a value between [-90.0 ~ 90.0] (degrees).
247 public double Latitude;
250 /// Longitude component of the co-ordinate.
251 /// Should have a value between [-180.0 ~ 180.0] (degrees).
253 public double Longitude;