Objective c &引用;从“出口”处走出100米;哈弗森公式

Objective c &引用;从“出口”处走出100米;哈弗森公式,objective-c,haversine,Objective C,Haversine,我对坐标感兴趣,我想知道,如何才能得到两点之间的距离(坐标),单位为米。经过长时间的搜索,我找到了哈弗森公式,以及它的Objective-C实现 如下所示(我为自己做了一点修改): 我的问题是: 如何才能获得当前位置的100米距离(纬度和经度)? 这个公式对我来说太复杂了,我不理解它,所以我不能“编码回去”得到我想要的结果 我只需要实际位置(巴黎、东京、伦敦、纽约,随便什么),一个(浮动)数字表示纬度,一个(浮动)数字表示经度,这个(浮动)数字表示距离实际位置100米的距离 如果你打开页面,在这

我对坐标感兴趣,我想知道,如何才能得到两点之间的距离(坐标),单位为米。经过长时间的搜索,我找到了哈弗森公式,以及它的Objective-C实现

如下所示(我为自己做了一点修改):

我的问题是: 如何才能获得当前位置的100米距离(纬度和经度)? 这个公式对我来说太复杂了,我不理解它,所以我不能“编码回去”得到我想要的结果

我只需要实际位置(巴黎、东京、伦敦、纽约,随便什么),一个(浮动)数字表示纬度,一个(浮动)数字表示经度,这个(浮动)数字表示距离实际位置100米的距离

如果你打开页面,在这里你可以“计算”出两点之间的100米距离(“实际”和100米之外的那个)

例如:

第1点:47.0,19.0 第二点:47.0,19.0013190 ---这是0.1000公里(100米)的距离

第1点:47.0,19.0 第2点:47.0008995,19.0 ---距离也为0.1000公里(100米)

在这里你可以看到,在坐标(纬度47.0和经度19.0)处,100米的距离是0.0008995(纬度)和0.0013190(经度)

我想借助哈弗森公式得到这些数据,只是不知道如何得到

你能帮我弄清楚吗

谢谢

更新: 谢谢你的回答,现在我没有时间尝试,但据我所知,我没有确切解释我想要什么。 也许这是一个更好的例子,我想如何使用这些“100米”:

现在我在坐标“lat x”和“lon y”处。在另一个给定坐标上还有另一个点(比如一家酒店),“lat a”和“lon b”。 我的问题是:如果这家酒店离我(不到)100米,我怎么计算?所以没关系,如果只有5米或99米,他们离我的距离都小于(或等于)100米

根据我提供的代码,我可以计算出来,这就是公式的用途

但比方说,我有一百万个其他坐标(酒店位置),我想与之合作。我只需要一份名单,离我不到100米的地方。是的,这是一个半径为100米的圆,我需要在这里面的结果

这需要更多的时间和资源来获取这些“百万”酒店的所有坐标,并一个接一个地计算距离,这就是为什么我认为计算出100米的纬度和经度要容易得多的原因(当我们在不同的位置时,改变值,这就是为什么我不能简单地使用我在上面的例子中计算出的值)。 因此,如果我想知道100米的经纬度,例如在伦敦的坐标(如果我在那里),我可以简单地通过一个简单的除法得到离我100米(小于)的酒店列表:

if 
((hotelLocation.coordinate.latitude <= (myLocation.coordinate.latitude + "100metersInLatitude")) || (hotelLocation.coordinate.latitude >= (myLocation.coordinate.latitude - "100metersInLatitude")))
&&
((hotelLocation.coordinate.longitude <= (myLocation.coordinate.longitude + "100metersInLongitude")) || (hotelLocation.coordinate.longitude >= (myLocation.coordinate.longitude - "100metersInLongitude")))
{
   NSLog(@"Be Happy :-) !");
}
if
((hotelLocation.coordinate.latitude=(myLocation.coordinate.latitude-“100米纬度”))
&&
((hotelLocation.coordinate.longitude=(myLocation.coordinate.longitude-“100米经度”))
{
NSLog(@“快乐:-)!”;
}
我只需要这些“100米纬度”和“100米经度”,总是从“我的位置”计算出来


哇,我希望,有人能理解我刚才写的内容,因为这对我来说并不容易,也不…:-))

假设你有一个纬度和经度的点,并且你想找到另一个点,即方位b上的距离d,当距离很小时(你说的“100米”在地球表面上非常小),那么你可以做一个简单的近似-将地球表面局部视为“平坦”。下面是一个简单的C程序,它实现了这一点(使用上面的数字)。我对其进行了更新,使其也包含了“精确”的公式——这只是一些额外的计算,但它在所有距离上都是精确的(不仅仅是短距离)。我使用的方程式来自于您引用的链接-副标题“给定距离和方向的终点到起点”

更新了-我将精确计算移到了一个单独的函数中,并添加了一个循环来计算0到359之间所有整数轴承的新点,每30次打印一次。这就是我在最初的评论中提到的“圈子”

#include <stdio.h>
#include <math.h>

double radians(double x) {
  return acos(0.0) * x / 90.0;
}

void calcNewPosition(double lat, double lon, double bearing, double d, double *newLat, double *newLon) {
  double lat1, lon1, br, pi;
  double Re = 6371000;
// convert everything to radians first:
  lat1 = radians(lat);
  lon1 = radians(lon);
  br = radians(bearing);

  pi = 2 * acos(0.0);

  double lat2, lon2;
  lat2 = asin( sin(lat1) * cos(d/Re) +
              cos( lat1 ) * sin( d / Re ) * cos(br ) );
  lon2 = lon1 + atan2(sin(br) * sin( d / Re ) * cos( lat1 ), \
    cos( d / Re ) - sin(lat1 ) * sin( lat2 ) );
  *newLat = 180. * lat2 / pi;
  *newLon = 180. * lon2 / pi;
}


int main(void) {
  double lon = 47., lat=19.;
  double newLongitude, newLatitude;
  double dx, dy, dLong, dLat;
  double Re = 6371000, d = 100, bearing = 0.0;
  double pi;
  double lat1, lon1, br;

  // convert everything to radians first:
  lat1 = radians(lat);
  lon1 = radians(lon);
  br = radians(bearing);

  pi = 2 * acos(0.0);

  // approximate calculation - using equirectangular approximation
  // and noting that distance between meridians (lines of longitude)
  // get closer at higher latitudes, with cos(latitude).
  dx = d * sin(br);                                // distance in E-W direction
  dy = d * cos(br);                                // distance in N-S direction
  dLat = 360 * dy / (2.0 * pi * Re);               // convert N-S to degrees latitude
  dLong = 360 * dx / (2.0 * pi * Re * cos(lat1));  // convert E-W to degrees longitude

  newLatitude = lat + dLat;
  newLongitude = lon + dLong;
  printf("simple forumula: the new position is %.8lf lon, %.8lf lat\n", newLongitude, newLatitude);

  // more accurate formula: based on http://www.movable-type.co.uk/scripts/latlong.html

  double lat2, lon2;
  calcNewPosition(lat, lon, bearing, d, &lat2, &lon2);
  printf("more accurate:   the new position is %.8lf lon, %.8lf lat\n", lon2, lat2);

  // now loop over all bearings and compute the "circle of points":
  int iBearing;
  double lonArray[360], latArray[360];
  for(iBearing = 0; iBearing < 360; iBearing++) {
    calcNewPosition(lat, lon, (double)iBearing, d, &latArray[iBearing], &lonArray[iBearing]);
    if (iBearing % 30 == 0) printf("bearing %03d: new lat = %.8lf, new lon = %.8lf\n", iBearing, latArray[iBearing], lonArray[iBearing]);
  }

return 0;
}

如您所见,精确到一米以内(您的代码给出19.0008995-实际上,您的结果可能在最后一位“错误”,因为这两种方法使用不同的方程式,但它们都是8位有效数字)。

如果OP需要一个距离的位置,这个问题实际上无法回答距离当前位置(100米)且未提供所需方位,在该点周围的圆圈中存在无限多个点

因此,这个答案可能是OP想要的,也可能不是OP想要的,它是一种使用
CLLocation
计算两点之间距离的方法

创建两个
CLLocation
点并使用方法
您是否查看了
CLLocation
方法
-(CLLocationDistance)distance fromLocation:(const CLLocation*)location`

CLLocation *location1 = [[CLLocation alloc] initWithLatitude:)latitude1 longitude:longitude1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:)latitude2 longitude:longitude2];
double distance = [location1 distanceFromLocation:location2];

地球在赤道处的半径=6371公里。赤道被划分为360度经度,因此赤道处的每个度代表大约111.32公里。从赤道向极点移动时,该距离在极点处减小为零。要计算不同纬度处的距离,将其乘以纬度的余弦

小数点后3位,约0.001度至 赤道处111.32米
北纬30度时为96.41米
北纬45度时78.71米
北纬60度时55.66米
北纬75度时为28.82米

小迪
simple forumula: the new position is 47.00000000 lon, 19.00089932 lat
more accurate:   the new position is 47.00000000 lon, 19.00089932 lat
bearing 000: new lat = 19.00089932, new lon = 47.00000000
bearing 030: new lat = 19.00077883, new lon = 47.00047557
bearing 060: new lat = 19.00044966, new lon = 47.00082371
bearing 090: new lat = 19.00000000, new lon = 47.00095114
bearing 120: new lat = 18.99955034, new lon = 47.00082371
bearing 150: new lat = 18.99922116, new lon = 47.00047557
bearing 180: new lat = 18.99910068, new lon = 47.00000000
bearing 210: new lat = 18.99922116, new lon = 46.99952443
bearing 240: new lat = 18.99955034, new lon = 46.99917629
bearing 270: new lat = 19.00000000, new lon = 46.99904886
bearing 300: new lat = 19.00044966, new lon = 46.99917629
bearing 330: new lat = 19.00077883, new lon = 46.99952443
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:)latitude1 longitude:longitude1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:)latitude2 longitude:longitude2];
double distance = [location1 distanceFromLocation:location2];
var x = (lng2-lng1) * cos((lat1+lat2)/2);
var y = (lat2-lat1);
var d = sqrt(x*x + y*y) * R;