在我的应用程序中,我试图获取两个位置之间的距离。我按下应用程序的按钮,穿过(大)房间,CLLocationDistance 返回为 0.000000。
.h 文件:
@property (strong, nonatomic) IBOutlet UIImageView *stopButton;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *firstLocation;
@property (strong, nonatomic) CLLocation *secondLocation;
@property (strong, nonatomic) IBOutlet UILabel *locationLabel;
.m 文件:
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
self.firstLocation = [self deviceLocation];
}
- myButtonFunction {
self.secondLocation = [self deviceLocation];
self.locationLabel.text = [[NSString alloc] initWithFormat:@"%f", [self.firstLocation distanceFromLocation:self.secondLocation]];
}
- (CLLocation *)deviceLocation {
return [[CLLocation alloc] initWithLatitude:self.locationManager.location.coordinate.latitude longitude:self.locationManager.location.coordinate.longitude];
}