iOS中位置之间的距离错误?

Posted

技术标签:

【中文标题】iOS中位置之间的距离错误?【英文标题】:Getting wrong distance between locations in iOS? 【发布时间】:2014-01-27 07:23:41 【问题描述】:

您好,使用以下方法来找出我当前位置与存储在 NSMutableArray 值中的位置(纬度和经度)之间的距离。

但是距离不对..请帮忙..

我的代码

    -(void)getDistancetoShow:(NSMutableArray *)newArray

    CLLocationCoordinate2D coordinateUser = [self getLocation];


    float _lat,_long;


  first_Loc = [[CLLocation alloc] initWithLatitude:coordinateUser.latitude longitude:coordinateUser.longitude];

    for(int p=0;p<[[newArray1 valueForKey:@"Name"] count];p++)

        _lat=[[[newArray1 valueForKey:@"Latitude"] objectAtIndex:p]floatValue];
         _long=[[[newArray1 valueForKey:@"Longitude"] objectAtIndex:p]floatValue];
        second_loc=[[CLLocation alloc] initWithLatitude:_lat longitude:_long];


        showDistance=[second_loc distanceFromLocation:first_Loc]/1000;

        [distanceToDispaly addObject:[NSString stringWithFormat:@"%.2f KM",showDistance]];
    

     NSLog(@"first=%@, second=%@", first_Loc, second_loc);



数组中的纬度

( "47.0735010448824", "47.0564688100431", " 47.0582514311038", "47.0587640538326", "47.0569233603454", "47.0541853132569", "47.0542029215138", "47.0544259594592", "47.0560264547367", " 47.0576532159776", " 47.0550023679218", "47.0342030007379", "47.0746263896213", " 47.0740256635512", "47.0524765957921", "47.0606287049051", "47.0539691521825", "47.0542799159057", "47.0651001682846", "47.0536948902097", "47.0525973335309", "47.0389265414812", "47.0761811267051", "47.0668801601942", "47.0614859079241", "47.0579433468181", “47.0718998779465” )

和数组中的经度

( "21.9154175327011", "21.9312065669748", "21.9337414545594", " 21.9346772505188", " 21.9300587945685", "21.9363460105132", "21.9362081709222", "21.9343042603097", "21.939485335992", "21.9320057169724", "21.9300799002643", "21.9485373571669", "21.9310667367526", "21.9318507902135", "21.9192195298473", "21.9195273899529", "21.9329595191441", "21.9292015418841", "21.9219452321208", "21.9098849252041", "21.9074768948561", "21.9424499491422", "21.9151458954504", "21.9304346568769", "21.9305973807911", "21.9331511189507", “21.9159872752442” )

但实际距离类似于盯着 9**** 但现在开始 5***

【问题讨论】:

您是否在模拟器中运行您的应用程序?模拟器无法显示您当前的位置 first_Loc 的值是多少?没有这些信息,就不可能检查您的结果。 您是否验证 first_loc 包含您期望的内容? - 当我用[second_loc distanceFromLocation: first_loc] 计算从first_loc = (12.975602,-77.638898)second_loc = (47.0735010448824, 21.9154175327011) 的距离时,结果是9656643.489674237。 @user2990885:您能否将NSLog(@"first=%@, second=%@", first_loc, second_loc); 添加到您的代码中并显示输出(仅显示第一个计算距离的输出)? @Jave:这不是那个问题的重复。如您所见,OP 正在使用正确的方法distanceFromLocation: 来计算距离。 【参考方案1】:

CLLocation 为您提供两个地方之间的乌鸦(直线)距离。我认为你的距离越来越远了。

首先获取两个地方的坐标并找到它们之间的距离。 然后搜索这两个坐标之间的乌鸦距离。

希望这会有所帮助

【讨论】:

精确距离就像 96222.00 但它只显示 56232.00 等 好的,首先你应该给出两点的硬编码坐标取两个 CLLocation 变量并应用 distanceFromPlayerToEnemySelectedInMeters = [enemyLocation distanceFromLocation:playerLocation];【参考方案2】:

一旦你得到这两个坐标,你就可以使用这段代码(取自here)计算它们之间的距离:

- (NSNumber*)calculateDistanceInMetersBetweenCoord:(CLLocationCoordinate2D)coord1 coord:(CLLocationCoordinate2D)coord2 
    NSInteger nRadius = 6371; // Earth's radius in Kilometers
    double latDiff = (coord2.latitude - coord1.latitude) * (M_PI/180);
    double lonDiff = (coord2.longitude - coord1.longitude) * (M_PI/180);
    double lat1InRadians = coord1.latitude * (M_PI/180);
    double lat2InRadians = coord2.latitude * (M_PI/180);
    double nA = pow ( sin(latDiff/2), 2 ) + cos(lat1InRadians) * cos(lat2InRadians) * pow ( sin(lonDiff/2), 2 );
    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
    double nD = nRadius * nC;
    // convert to meters
    return @(nD*1000);

希望这会有所帮助!

【讨论】:

CLLocation 有一个名为distanceFromLocation: 的方法返回以米为单位的距离 你怎么知道真正的距离? 我在谷歌搜索它显示距离是 9621KM 并且我只得到 5646509.944427(以米为单位)。当我第一次实现我的代码时,它显示为 9621212.245445(meter) 但我试图除以 1000。之后我删除了那个潜水部分但它仍然给出了错误的值 备注:我不知道distanceFromLocation: 是如何计算距离的,但是“Haversine 公式”(这是您的calculateDistanceInMetersBetweenCoord: 使用的)不能对地球上的所有位置都精确,因为地球的半径不是恒定的。 @MartinR 你是对的。由于这不是我的代码,我没有意识到这种方法的潜在误差高达 0.5%。 +1【参考方案3】:

要从Swift 中的点数组获取距离,请使用以下 reduce 方法。

这里locationsCLLocation 类型的数组。

 let calculatedDistance = locations.reduce((0, locations[0]))  ($0.0 + $0.1.distance(from: $1), $1).0

【讨论】:

以上是关于iOS中位置之间的距离错误?的主要内容,如果未能解决你的问题,请参考以下文章

用于查找使用纬度和经度之间的距离的Sql返回错误的距离

如何使用它们的经度和纬度值计算两个位置之间的距离

使用经纬度查找距离的Sql返回错误的距离

创建随机位置时距离错误

找到带有haversine错误的pandas中2个坐标之间的距离

CLLocationDistance,两个纬度和经度之间的英里和英尺给我iOS的错误值?