GMSGeometryUtils.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // GMSGeometryUtils.h
  3. // Google Maps SDK for iOS
  4. //
  5. // Copyright 2013 Google Inc.
  6. //
  7. // Usage of this SDK is subject to the Google Maps/Google Earth APIs Terms of
  8. // Service: https://developers.google.com/maps/terms
  9. //
  10. /**
  11. * \defgroup GeometryUtils GMSGeometryUtils
  12. * @{
  13. */
  14. #import <CoreLocation/CoreLocation.h>
  15. #import <GoogleMaps/GMSPath.h>
  16. #define OVERLOADABLE __attribute__((overloadable))
  17. @class GMSPath;
  18. /** Average Earth radius in meters. */
  19. static const CLLocationDistance kGMSEarthRadius = 6371009.0;
  20. /**
  21. * A point on the map. May represent a projected coordinate. x is in [-1, 1].
  22. * The axis direction is normal: y grows towards North, x grows towards East.
  23. * (0, 0) is the center of the map. See GMSProject() and GMSUnproject().
  24. */
  25. typedef struct GMSMapPoint {
  26. double x;
  27. double y;
  28. } GMSMapPoint;
  29. /** Projects |coordinate| to the map. |coordinate| must be valid. */
  30. FOUNDATION_EXPORT
  31. GMSMapPoint GMSProject(CLLocationCoordinate2D coordinate);
  32. /** Unprojects |point| from the map. point.x must be in [-1, 1]. */
  33. FOUNDATION_EXPORT
  34. CLLocationCoordinate2D GMSUnproject(GMSMapPoint point);
  35. /**
  36. * Returns a linearly interpolated point on the segment [a, b], at the fraction
  37. * |t| from |a|. |t|==0 corresponds to |a|, |t|==1 corresponds to |b|.
  38. * The interpolation takes place along the short path between the points
  39. * potentially crossing the date line. E.g. interpolating from San Francisco
  40. * to Tokyo will pass north of Hawaii and cross the date line.
  41. */
  42. FOUNDATION_EXPORT
  43. GMSMapPoint GMSMapPointInterpolate(GMSMapPoint a, GMSMapPoint b, double t);
  44. /**
  45. * Returns the length of the segment [a, b] in projected space. The length is
  46. * computed along the short path between the points potentially crossing the
  47. * date line. E.g. the distance between the points corresponding to
  48. * San Francisco and Tokyo measures the segment that passes north of Hawaii
  49. * crossing the date line.
  50. */
  51. FOUNDATION_EXPORT
  52. double GMSMapPointDistance(GMSMapPoint a, GMSMapPoint b);
  53. /**
  54. * Returns whether |point| lies inside of path. The path is always cosidered
  55. * closed, regardless of whether the last point equals the first or not.
  56. * Inside is defined as not containing the South Pole -- the South Pole is
  57. * always outside.
  58. * |path| describes great circle segments if |geodesic| is YES, and rhumb
  59. * (loxodromic) segments otherwise.
  60. * If |point| is exactly equal to one of the vertices, the result is YES.
  61. * A point that is not equal to a vertex is on one side or the other of any path
  62. * segment -- it can never be "exactly on the border".
  63. * See GMSGeometryIsLocationOnPath() for a border test with tolerance.
  64. */
  65. FOUNDATION_EXPORT
  66. BOOL GMSGeometryContainsLocation(CLLocationCoordinate2D point, GMSPath *path,
  67. BOOL geodesic);
  68. /**
  69. * Returns whether |point| lies on or near |path|, within the specified
  70. * |tolerance| in meters.
  71. * |path| is composed of great circle segments if |geodesic| is YES, and of
  72. * rhumb (loxodromic) segments if |geodesic| is NO.
  73. * See also GMSGeometryIsLocationOnPath(point, path, geodesic).
  74. *
  75. * The tolerance, in meters, is relative to the spherical radius of the Earth.
  76. * If you need to work on a sphere of different radius,
  77. * you may compute the equivalent tolerance from the desired tolerance on the
  78. * sphere of radius R: tolerance = toleranceR * (RadiusEarth / R),
  79. * with RadiusEarth==6371009.
  80. */
  81. FOUNDATION_EXPORT OVERLOADABLE
  82. BOOL GMSGeometryIsLocationOnPath(CLLocationCoordinate2D point, GMSPath *path,
  83. BOOL geodesic, CLLocationDistance tolerance);
  84. /**
  85. * Same as GMSGeometryIsLocationOnPath(point, path, geodesic, tolerance),
  86. * with a default tolerance of 0.1 meters.
  87. */
  88. FOUNDATION_EXPORT OVERLOADABLE
  89. BOOL GMSGeometryIsLocationOnPath(CLLocationCoordinate2D point, GMSPath *path,
  90. BOOL geodesic);
  91. /**
  92. * Returns the great circle distance between two coordinates, in meters,
  93. * on Earth.
  94. * This is the shortest distance between the two coordinates on the sphere.
  95. * Both coordinates must be valid.
  96. */
  97. FOUNDATION_EXPORT OVERLOADABLE
  98. CLLocationDistance GMSGeometryDistance(CLLocationCoordinate2D from,
  99. CLLocationCoordinate2D to);
  100. /**
  101. * Returns the great circle length of |path|, in meters, on Earth.
  102. * This is the sum of GMSGeometryDistance() over the path segments.
  103. * All the coordinates of the path must be valid.
  104. */
  105. FOUNDATION_EXPORT OVERLOADABLE
  106. CLLocationDistance GMSGeometryLength(GMSPath *path);
  107. /**
  108. * Returns the area of a geodesic polygon defined by |path| on Earth.
  109. * The "inside" of the polygon is defined as not containing the South pole.
  110. * If |path| is not closed, it is implicitly treated as a closed path
  111. * nevertheless and the result is the same.
  112. * All coordinates of the path must be valid.
  113. * If any segment of the path is a pair of antipodal points, the
  114. * result is undefined -- because two antipodal points do not form a
  115. * unique great circle segment on the sphere.
  116. * The polygon must be simple (not self-overlapping) and may be concave.
  117. */
  118. FOUNDATION_EXPORT OVERLOADABLE
  119. double GMSGeometryArea(GMSPath *path);
  120. /**
  121. * Returns the signed area of a geodesic polygon defined by |path| on Earth.
  122. * The result has the same absolute value as GMSGeometryArea(); it is positive
  123. * if the points of path are in counter-clockwise order, and negative otherwise.
  124. * The same restrictions as on GMSGeometryArea() apply.
  125. */
  126. FOUNDATION_EXPORT OVERLOADABLE
  127. double GMSGeometrySignedArea(GMSPath *path);
  128. /**
  129. * Returns the initial heading (degrees clockwise of North) at |from|
  130. * of the shortest path to |to|.
  131. * Returns 0 if the two coordinates are the same.
  132. * Both coordinates must be valid.
  133. * The returned value is in the range [0, 360).
  134. *
  135. * To get the final heading at |to| one may use
  136. * (GMSGeometryHeading(|to|, |from|) + 180) modulo 360.
  137. */
  138. FOUNDATION_EXPORT
  139. CLLocationDirection GMSGeometryHeading(CLLocationCoordinate2D from,
  140. CLLocationCoordinate2D to);
  141. /**
  142. * Returns the destination coordinate, when starting at |from|
  143. * with initial |heading|, travelling |distance| meters along a great circle
  144. * arc, on Earth.
  145. * The resulting longitude is in the range [-180, 180).
  146. * Both coordinates must be valid.
  147. */
  148. FOUNDATION_EXPORT OVERLOADABLE
  149. CLLocationCoordinate2D GMSGeometryOffset(CLLocationCoordinate2D from,
  150. CLLocationDistance distance,
  151. CLLocationDirection heading);
  152. /**
  153. * Returns the coordinate that lies the given |fraction| of the way between
  154. * the |from| and |to| coordinates on the shortest path between the two.
  155. * The resulting longitude is in the range [-180, 180).
  156. */
  157. FOUNDATION_EXPORT
  158. CLLocationCoordinate2D GMSGeometryInterpolate(CLLocationCoordinate2D from,
  159. CLLocationCoordinate2D to,
  160. double fraction);
  161. /**
  162. * Returns an NSArray of GMSStyleSpan constructed by repeated application of style and length
  163. * information from |styles| and |lengths| along |path|.
  164. *
  165. * |path| the path along which the output spans are computed.
  166. * |styles| an NSArray of GMSStrokeStyle. Wraps if consumed. Can't be empty.
  167. * |lengths| an NSArray of NSNumber; each entry gives the length of the corresponding
  168. * style from |styles|. Wraps if consumed. Can't be empty.
  169. * |lengthKind| the interpretation of values from |lengths| (geodesic, rhumb or projected).
  170. *
  171. * Example: a polyline with alternating black and white spans:
  172. *
  173. * <pre>
  174. * GMSMutablePath *path;
  175. * NSArray *styles = @[[GMSStrokeStyle solidColor:[UIColor whiteColor]],
  176. * [GMSStrokeStyle solidColor:[UIColor blackColor]]];
  177. * NSArray *lengths = @[@100000, @50000];
  178. * polyline.path = path;
  179. * polyline.spans = GMSStyleSpans(path, styles, lengths, kGMSLengthRhumb);
  180. * </pre>
  181. */
  182. FOUNDATION_EXPORT OVERLOADABLE
  183. NSArray *GMSStyleSpans(GMSPath *path, NSArray *styles, NSArray *lengths, GMSLengthKind lengthKind);
  184. /**
  185. * Similar to GMSStyleSpans(path, styles, lengths, lengthKind) but additionally takes an initial
  186. * length offset that will be skipped over relative to the |lengths| array.
  187. *
  188. * |lengthOffset| the length (e.g. in meters) that should be skipped initially from |lengths|.
  189. */
  190. FOUNDATION_EXPORT OVERLOADABLE
  191. NSArray *GMSStyleSpans(GMSPath *path, NSArray *styles, NSArray *lengths, GMSLengthKind lengthKind,
  192. double lengthOffset);
  193. /**@}*/