在应用程序中,我需要计算两点之间的距离。我是这样做的。
- (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
您可以看到,这些位置彼此靠近。但它仍然说我们太遥远了。有人可以帮帮我吗?
答案 0 :(得分:1)
你为什么要用这个:
如果它超过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");
}
}
}
或者是corse:
if(meters > 5000){
NSLog(@"it's far enough");
}else{
NSLog(@"too close");
}
还有一件非常重要的事情:
此方法通过跟踪测量两个位置之间的距离 它们之间的一条线跟随地球的曲率。该 产生的弧线是一条平滑的曲线并没有考虑在内 两个位置之间的特定高度变化。 来自apple documentation
distanceFromLocation测量“在空中”的距离,而不是逐个测量的距离。
答案 1 :(得分:0)
这是解决方案 -
-(NSString *)getDistance:(NSDictionary *)dicVal{
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[[dicVal valueForKey:@"latitude"] doubleValue] longitude:[[dicVal valueForKey:@"longitude"] doubleValue]];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:appDelegate.userLocation.latitude longitude:appDelegate.userLocation.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
NSString *dist=[NSString stringWithFormat:@"%4.0f %@", distance/1000,NSLocalizedString(@"KM", nil)];
return dist;
}
答案 2 :(得分:0)
是的,它只是每次cllocation
经理必须分配的分配问题,如下所示:
LocationManager = [[[CLLocationManager alloc] init] autorelease];
LocationManager.delegate = self;
LocationManager.desiredAccuracy = kCLLocationAccuracyBest;
LocationManager.distanceFilter = kCLDistanceFilterNone;
[LocationManager startUpdatingLocation];
CLLocation *location = [LocationManager location];
CLLocationCoordinate2D coordinate1 = [location coordinate];
CLLocation *currentLoc = [[CLLocation alloc] initWithLatitude:coordinate1.latitude longitude:coordinate1.longitude];
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:l@"Your latitude not current" longitude:@"Your longitude not current'];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:currentLoc];
if (meters > 1000)
{
Distancelbl.text=[NSString stringWithFormat: @"You are Away %.2f Km",meters];
}
else{
Distancelbl.text=[NSString stringWithFormat: @"You are Away %.2f Mtr",meters];
}
`