首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何比较CurrentLocation和GPS坐标,如果相等则显示消息?

如何比较CurrentLocation和GPS坐标,如果相等则显示消息?
EN

Stack Overflow用户
提问于 2015-11-29 04:44:11
回答 1查看 365关注 0票数 2

我正在尝试创建一个最终会在特定位置振动的应用程序。这周我刚开始学习Objective-C,发现这个教程对初始测试很有用:http://www.appcoda.com/how-to-get-current-location-iphone-user/

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

代码语言:javascript
复制
//  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

如果有帮助的话,这里是.h:

代码语言:javascript
复制
//  ViewController.h

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
- (IBAction)getCurrentLocation:(id)sender;

@end

我也尝试过这样做:

代码语言:javascript
复制
if (currentLocation != nil) {
    longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}

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

有什么办法解释为什么这个不起作用吗?如果这个问题已经在某处被报道了,我很抱歉,我已经花了几个小时在网上搜索和尝试解决方案,但由于我是新手,我可能不知道要搜索的正确术语。任何建议都是非常感谢的!

EN

回答 1

Stack Overflow用户

发布于 2015-11-29 05:49:40

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

代码语言:javascript
复制
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);
}

此外,为了确保设置标签值是在主线程上执行的,您可以像这样包装这些调用:

代码语言:javascript
复制
dispatch_async(dispatch_get_main_queue(), ^ {
    longitudeLabel.text = ...;
    latitudeLabel.text = ....;
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33976262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档