Ios 如何将当前位置与GPS坐标进行比较,如果相等,如何显示消息?

Ios 如何将当前位置与GPS坐标进行比较,如果相等,如何显示消息?,ios,objective-c,core-location,Ios,Objective C,Core Location,我正在尝试创建一个最终会在特定位置震动的应用程序。本周我刚开始学习Objective-C,发现本教程似乎对初始测试有用: 我不是在振动,而是在测试它,以便标签在指定的坐标处发生变化。但是,当我运行应用程序时,它不会更改标签。在xCode的底部调试输出栏中,它显示了坐标,它们与我想要的坐标相同,但是没有将标签“For Latitude Value”更改为“Success”,它什么也没有更改。以下是当前代码: // ViewController.m #import "ViewController

我正在尝试创建一个最终会在特定位置震动的应用程序。本周我刚开始学习Objective-C,发现本教程似乎对初始测试有用:

我不是在振动,而是在测试它,以便标签在指定的坐标处发生变化。但是,当我运行应用程序时,它不会更改标签。在xCode的底部调试输出栏中,它显示了坐标,它们与我想要的坐标相同,但是没有将标签“For Latitude Value”更改为“Success”,它什么也没有更改。以下是当前代码:

//  ViewController.m

#import "ViewController.h"
@import CoreLocation;

@interface ViewController ()<CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLGeocoder *geocoder;
@property (strong, nonatomic) CLPlacemark *placemark;

@end

@implementation ViewController {
CLLocationManager *locationManager;
}

- (void)requestAlwaysAuthorization{

}

 - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    locationManager = [[CLLocationManager alloc] init];
    if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [locationManager requestAlwaysAuthorization];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@", [locations lastObject]);
}

- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateLocations: %@", newLocation);
CLLocation *currentLocation = newLocation;

if ((currentLocation.coordinate.longitude == 51.50998000)&&(currentLocation.coordinate.latitude == -0.13370000)) {
    self.longitudeLabel.text = [NSString stringWithFormat:@"success"];
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
就像他们在教程中所做的那样,它应该更改标签以显示GPS坐标,但它们也不会更改。同样,它只是在调试栏中显示GPS坐标


你知道为什么这样不行吗?我很抱歉,如果这已经涵盖了一些地方,我已经花了几个小时在网上搜索和尝试解决方案,但由于我是新手,我可能不知道正确的搜索术语。非常感谢您的建议

我不建议比较两个双值是否相等。理论上,他们永远不会平等。更好的方法是检查该位置是否足够靠近您的参考位置。下面是我代码库中的一个示例。这是我与实用程序函数一起使用的一个C文件。在这里,它处理浮点值,但可以对双值执行相同的操作

int relativelyClose(CGFloat value, CGFloat reference, CGFloat precision) 
{
    return fabs(value - reference) / reference < range;
}

int relativelyClosePoints(CGPoint point, CGPoint r, CGFloat precision) 
{
    CGFloat dx = point.x -r.x;
    CGFloat dy = point.y-r.y;
    CGFloat distance = sqrt(dx*dx + dy*dy);
    CGFloat reflength = sqrt(r.x*r.x + r.y*r.y);
    return (distance < reflength * precision);
}

你能确认调用了
locationManager:didUpdateToLocation:fromLocation:
方法吗?好问题-我不完全确定我该怎么做,但如果那不起作用,这可能解释了问题。我尝试添加NSLog(@“didUpdateLocations:%@”,fromLocation);在NSLog下(@“didUpdateLocations:%@”,newLocation);,但这导致生成失败。您不能使用
%@
格式化
CLLocation
,因为
CLLocation
不是类,它是一个
struct
。是否有可能因为要调用函数,新位置必须与旧位置不同而不调用它?(不确定是否是这种情况,但我无法从代码中辨别。)我并不真正关心比较新旧位置,我只希望将当前位置与指定坐标进行比较。因此,我是否应该更改函数
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation
?谢谢Rudi,我很感激!我本来打算让它最终接受足够接近的值,所以我还是现在就做吧。干杯不幸的是,它不会影响当前的问题,但它肯定是一个很好的补充。如果驱动蜂鸣器工作,但更新UI不工作,那么可能是从主线程以外的另一个线程调用委托方法。对UI的更改必须在主线程上执行。您可以通过如下方式包装文本设置代码来确保在主线程上执行:我无法在此处添加代码,因此我将编辑我的答案以包含此代码。
if (currentLocation != nil) {
    longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
int relativelyClose(CGFloat value, CGFloat reference, CGFloat precision) 
{
    return fabs(value - reference) / reference < range;
}

int relativelyClosePoints(CGPoint point, CGPoint r, CGFloat precision) 
{
    CGFloat dx = point.x -r.x;
    CGFloat dy = point.y-r.y;
    CGFloat distance = sqrt(dx*dx + dy*dy);
    CGFloat reflength = sqrt(r.x*r.x + r.y*r.y);
    return (distance < reflength * precision);
}
dispatch_async(dispatch_get_main_queue(), ^ {
    longitudeLabel.text = ...;
    latitudeLabel.text = ....;
});