在应用程序中,我需要计算两点之间的距离。我就是这样
- (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");
}
还有一件非常重要的事情:
该方法通过跟踪来测量两个位置之间的距离
它们之间的一条线跟随地球的曲率。的
产生的圆弧是平滑曲线,未考虑在内
两个位置之间的特定高度变化。
来自apple documentation
distanceFromLocation测量“空中”的距离,而不是逐圈测量。
关于iphone - distanceFromLocation似乎不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19428832/