问题
I'm finishing an App in wish i need to show the user the distance between him and about 500 coordinates.
using the CLLocation method to calculate it, works well, but it takes about 1 minute in iPhone 4 to finish calculations for each location.
What is the best way to do it? Using span? Any other faster way?
Thanks all,
rui
回答1:
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:
I have seen the other answers, don't know if they are right, but I think there is a better solution:
(from documentation):
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
You can use it like this:
- (CLLocationDistance) DistanceBetweenCoordinate:(CLLocationCoordinate2D)originCoordinate andCoordinate:(CLLocationCoordinate2D)destinationCoordinate {
CLLocation *originLocation = [[CLLocation alloc] initWithLatitude:originCoordinate.latitude longitude:originCoordinate.longitude];
CLLocation *destinationLocation = [[CLLocation alloc] initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
CLLocationDistance distance = [originLocation distanceFromLocation:destinationLocation];
[originLocation release];
[destinationLocation release];
return distance;
}
回答3:
Here is code for that..
-(NSString *)findDistanceBetweenTwoLatLon
{
int intEarthRadius = 3963;
double dblLat1 = DegreesToRadians(firstLatitude);
double dblLon1 = DegreesToRadians(firstLongitude);
double dblLat2 = DegreesToRadians(secondLatitude);
double dblLon2 = DegreesToRadians(secondLongitude);
float fltLat = dblLat2 - dblLat1;
float fltLon = dblLon2 - dblLon1;
double a = sin(fltLat/2) * sin(fltLat/2) + cos(dblLat2) * cos(dblLat2) * sin(fltLon/2) * sin(fltLon/2) ;
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = intEarthRadius * c;
double dMeters = d * kOneMileMeters;
NSString *strDistance = [NSString stringWithFormat:@"%1.2f meters",dMeters];
return strDistance;
}
Define all this Macro..
and for degrees to radians
#define DegreesToRadians(degrees) (degrees * M_PI / 180)
where M_PI
#define M_PI 3.14159265358979323846264338327950288
#define kOneMileMeters 1609.344
回答4:
You can try another approach.You can fetch the lat-long of both the points(assuming lat-long finding will not take that much time) and there are formulae to calculate the distance using lat-long.
来源:https://stackoverflow.com/questions/8877756/calculate-distance-between-2-coordinates-iphone-best-practice