我试图创建一个最终会在某些位置振动的应用。我本周刚开始学习Objective-C,发现本教程对初始测试很有用:http://www.appcoda.com/how-to-get-current-location-iphone-user/
不是振动,而是进行测试,以便标签在指定的坐标处发生变化。但是,当我运行应用程序时,它不会更改标签。在xCode的底部调试输出栏中,它显示坐标,它们与我想要的相同,但不是更改标签" For Latitude Value"成功"成功"它什么都没改变。这是当前的代码:
// 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。
// 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
我也尝试过:
if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
和他们在教程中一样,它应该更改标签以显示GPS坐标,但它们也不会改变。同样,它只是在调试栏中显示GPS坐标。
关于为什么这不起作用的任何想法?我很抱歉,如果已经在某个地方覆盖了这个问题,我花了几个小时在网上搜索和尝试解决方案,但由于我是新手,我可能不知道要搜索的正确术语。非常感谢任何建议!
答案 0 :(得分:0)
我不建议比较两个双重值是否相等。从理论上讲,它们永远不会是平等的。更好的方法是检查位置是否足够接近您的参考位置。下面是我的代码库中的一个例子。这是我用于实用程序功能的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);
}
此外,为了确保在主线程上执行设置标签值,您可以像这样包装这些调用:
dispatch_async(dispatch_get_main_queue(), ^ {
longitudeLabel.text = ...;
latitudeLabel.text = ....;
});