【发布时间】:2021-03-17 12:26:17
【问题描述】:
我正在尝试在我的示例应用中实现经度和纬度。我使用 iPhone 5s 设备进行测试。它不会对按钮操作上的纬度和经度标签产生任何影响。
在编译 Xcode 之前在 didUpdateToLocation: fromLocation: method as 行显示警告
“实现不推荐使用的方法”
请帮助我用合适的方法代替此方法以使应用程序顺利运行
使用 Xcode 12.4 和 iOS 12.3 使用 Objective c 我正在尝试实现一个显示经度和纬度的简单位置演示应用程序。下面是我的 viewController.h 和 viewController.m 文件。我已经尝试了以下来自在线教程AppCoda的代码
viewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController :UIViewController<CLLocationManagerDelegate>
{
IBOutlet UILabel *lattitudeLabel;
IBOutlet UILabel *longitudeLabel;
IBOutlet UILabel *addressLabel;
}
- (IBAction)getCurrentLocation:(UIButton *)sender;
@end
viewController.m
#import "ViewController.h"
@interface ViewController ()
{
CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc]init];
geocoder = [[CLGeocoder alloc] init];
}
- (IBAction)getCurrentLocation:(UIButton *)sender
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
NSLog(@"Error: Failed to get location");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
lattitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
[locationManager stopUpdatingLocation];
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
self->placemark = [placemarks lastObject];
self->addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
self->placemark.subThoroughfare, self->placemark.thoroughfare,
self->placemark.postalCode, self->placemark.locality,
self->placemark.administrativeArea,
self->placemark.country];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
@end
点击按钮获取我的位置没有发生任何事情。它实际上应该更新除经度之外的坐标:和纬度:一些浮点值..但它没有这样做..我会问请建议我链接到任何教程网站或任何 GitHub 项目的链接,以便在 iOS 12.3 和以上目标c...感谢您的回复和建议的编辑。
【问题讨论】:
-
欢迎来到 SO。请edit您的问题并添加minimal reproducible example,显示您已经尝试过的内容以及哪些部分不起作用。另见How to Ask。
-
嗨@koen 感谢您的编辑建议。在为我接下来的所有问题提问之前,我会遵循所有指南。
标签: ios objective-c xcode