在应用程序中,我需要计算两点之间的距离。我就是这样做的。
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
NSLog(@"updating");
//Getting my concerts
arrConcerten = [matches mutableCopy];
for (Event *concert in arrConcerten) {
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:[concert.eve_latitude floatValue] longitude:[concert.eve_longitude floatValue]];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:newLocation];
NSLog(@"meters are %f",meters);
if(meters > 5000){
[locationManager2 stopUpdatingLocation];
NSString *message2 = [NSString stringWithFormat:@"Long = %f and lat= %f and Concert Lat = %f and long = %f",newLocation.coordinate.longitude,newLocation.coordinate.latitude,[concert.eve_latitude floatValue],[concert.eve_longitude floatValue]];
NSLog(@"%@",message2);
}else{
NSLog(@"too far away");
}
}
}
这就是我的日志所说的。
2013-10-17 15:57:23.518 Domino[7468:907] meters are 7973.753294
2013-10-17 15:57:23.520 Domino[7468:907] Long = 5.621456 and lat= 51.097340 and Concert Lat = 51.038139 and long = 5.557330
2013-10-17 15:57:23.896 Domino[7468:907] meters are 27850.874245
2013-10-17 15:57:23.899 Domino[7468:907] Long = 5.621456 and lat= 51.097340 and Concert Lat = 50.860332 and long = 5.493722
如您所见,这些位置彼此靠近。但它仍然说我们距离太远了。有人可以帮助我吗?
最佳答案
你为什么要用这个:
如果超过 5000 米:做点什么,否则太远了?我不明白其中的逻辑。如果距离太远,则超过 5000 米。所以我认为:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
NSLog(@"updating");
//Getting my concerts
arrConcerten = [matches mutableCopy];
for (Event *concert in arrConcerten) {
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:[concert.eve_latitude floatValue] longitude:[concert.eve_longitude floatValue]];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:newLocation];
NSLog(@"meters are %f",meters);
if(meters <= 5000){
[locationManager2 stopUpdatingLocation];
NSString *message2 = [NSString stringWithFormat:@"Long = %f and lat= %f and Concert Lat = %f and long = %f",newLocation.coordinate.longitude,newLocation.coordinate.latitude,[concert.eve_latitude floatValue],[concert.eve_longitude floatValue]];
NSLog(@"%@",message2);
}else{
NSLog(@"too far away");
}
}
}
或科西斯:
if(meters > 5000){
NSLog(@"it's far enough");
}else{
NSLog(@"too close");
}
还有一件非常重要的事情:
This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations. from apple documentation
distanceFromLocation 测量“空中”的距离,而不是逐个测量。
关于iphone - distanceFromLocation 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19428832/