从X meter的单个cllocation坐标创建垂直lat

时间:2016-04-04 12:22:55

标签: ios objective-c cllocationmanager cllocationcoordinate2d

我有用户当前位置,即CLLocation坐标(位置lat& long),用户在赛道上指向一个方向,在用户当前位置的帮助下我创建了一个区域现在我想要一些更多的赛道坐标(比如说2m)距垂直方向的赛道4m,6m),赛道长10米。请检查图像,红点在轨道上。 Please check this image

1 个答案:

答案 0 :(得分:0)

/**
 * Returns the destination point from initial point having travelled the given distance on the
 * given initial bearing (bearing normally varies around path followed).
 *
 * @param   {double} distance - Distance travelled, in same units as earth radius (default: metres).
 * @param   {double} bearing - Initial bearing in degrees from north.
 *
 * @returns {CLLocationCoordinate} Destination point.
 */

#define kEarthRadius 6378137

- (CLLocationCoordinate2D)destinationPointWithStartingPoint:(MKMapPoint)initialPoint distance:(double)distance andBearing:(double)bearing {
  CLLocationCoordinate2D location = MKCoordinateForMapPoint(initialPoint);

  double delta = distance / kEarthRadius;
  double omega = [self degreesToRadians:bearing];

  double phi1 = [self degreesToRadians:location.latitude];
  double lambda1 = [self degreesToRadians:location.longitude];

  double phi2 = asin(sin(phi1)*cos(delta) + cos(phi1) * sin(delta) * cos(omega));
  double x = cos(delta) - sin(phi1) * sin(phi2);
  double y = sin(omega) * sin(delta) * cos(phi1);
  double lambda2 = lambda1 + atan2(y, x);

  return CLLocationCoordinate2DMake([self radiansToDegrees:phi2], ([self radiansToDegrees:lambda2]+540)%360-180);
}

- (CLLocationCoordinate2D)rhumbDestinationPointForInitialPoint:(MKMapPoint)initialPoint distance:(double)distance andBearing:(double)bearing {
  CLLocationCoordinate2D location = MKCoordinateForMapPoint(initialPoint);

  double delta = distance / kEarthRadius;
  double omega = [self degreesToRadians:bearing];

  double phi1 = [self degreesToRadians:location.latitude];
  double lambda1 = [self degreesToRadians:location.longitude];

  double delta_phi = delta * cos(omega);
  double phi2 = phi1 + delta_phi;

  // check for some daft bugger going past the pole, normalise latitude if so
  if (fabs(phi2) > M_PI / 2) {
    phi2 = phi2 > 0 ? M_PI-phi2 : -M_PI-phi2;
  }

  double delta_gamma = log(tan(phi2/2+M_PI/4)/tan(phi1/2+M_PI/4));
  double q = fabs(delta_gamma) > 10e-12 ? delta_phi / delta_gamma : cos(phi1);

  double delta_lambda = delta*sin(omega)/q;
  double lambda2 = lambda1 + delta_lambda;

  return CLLocationCoordinate2DMake([self radiansToDegrees:phi2], ([self radiansToDegrees:lambda2]+540)%360-180);
}

- (double)degreesToRadians:(double)degrees {
  return degrees * M_PI / 180.0;
}

- (double)radiansToDegrees:(double)radians {
  return radians * 180.0 / M_PI;
}

改编自:http://www.movable-type.co.uk/scripts/latlong.html
有关轴承的更多信息:https://en.wikipedia.org/wiki/Bearing_(navigation)
和直线:https://en.wikipedia.org/wiki/Rhumb_line