计算2坐标之间的距离iphone - 最佳实践 [英] calculate distance between 2 coordinates iphone - best practice

查看:160
本文介绍了计算2坐标之间的距离iphone - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成一个应用程序希望我需要向用户显示他与大约500个坐标之间的距离。

I'm finishing an App in wish i need to show the user the distance between him and about 500 coordinates.

使用CLLocation方法计算它,有效好吧,但iPhone 4中大约需要1分钟才能完成每个位置的计算。

using the CLLocation method to calculate it, works well, but it takes about 1 minute in iPhone 4 to finish calculations for each location.

最好的方法是什么?用span?还有其他更快的方式吗?

What is the best way to do it? Using span? Any other faster way?

全部谢谢,

rui

推荐答案

我认为Sahgal是对的,这里有一些代码,也许它会帮助你。

I think Sahgal is right, here is some code, perhaps it will help you.

+(CGFloat)calculateDistanceBetweenSource:(CLLocationCoordinate2D)firstCoords andDestination:(CLLocationCoordinate2D)secondCoords 
{

    // this radius is in KM => if miles are needed it is calculated during setter of Place.distance

    double nRadius = 6371;

    // Get the difference between our two points

    // then convert the difference into radians

    double nDLat = (firstCoords.latitude - secondCoords.latitude)* (M_PI/180);
    double nDLon = (firstCoords.longitude - secondCoords.longitude)* (M_PI/180);

    double nLat1 =  secondCoords.latitude * (M_PI/180);
    double nLat2 =  secondCoords.latitude * (M_PI/180);

    double nA = pow ( sin(nDLat/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon/2), 2 );

    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));

    double nD = nRadius * nC;

    NSLog(@"Distance is %f",nD);

    return nD; // converts to miles or not (if en_) => implicit in method
}

这篇关于计算2坐标之间的距离iphone - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆