GMSGeometryUtils.h 8.4 KB

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