GMSGeometryUtils.h 8.9 KB

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