[英]How to stop multiple times method calling of didUpdateLocations() in ios
[英]didUpdateLocationsTo: from: method is deprecated for latest iOS versions(12.4). How to replace it with didUpdateLocations?
我正在尝试在我的示例应用程序中实现经度和纬度。 我使用 iPhone 5s 设备进行测试。 它对按钮操作的纬度和经度标签没有任何影响。
在编译 Xcode 之前在 didUpdateToLocation: fromLocation: 方法行显示警告为
“实施弃用的方法”
请帮助我用合适的方法代替此方法以使应用程序顺利运行
使用 Xcode 12.4 和 iOS 12.3 使用目标 c 我正在尝试实现一个简单的位置演示应用程序,它显示经度和纬度。 下面是我的 viewController.h 和 viewController.m 文件。 我已经尝试了以下来自在线教程AppCoda的代码
视图控制器.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
视图控制器.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
单击按钮获取我的位置没有任何事情发生。 它实际上应该更新除经度之外的坐标:和纬度:一些浮点值..但它没有这样做..我会问请建议我链接到任何教程 web 站点或任何 GitHub 项目中的位置处理和 Z1BDF605039194CDB11以上目标 c...感谢您的回复和建议的编辑。
正如文档所说,只需使用locationManager(_:didUpdateLocations:) 。
Swift 5
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Stop updates if this is a one-time request
manager.stopUpdatingLocation()
// Pop the last location off if you just need current update
guard let newLocation = locations.last else { return }
<... Do something with the location ...>
}
Objective-C
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
[manager stopUpdatingLocation];
CLLocation *newLocation = [locations lastObject];
if (newLocation) {
<... Do something with the location ...>
}
}
我希望这个委托方法的实现会对你有所帮助。 适用于 Mac 和 IOS。 我用它来确定当前的 position 用于天气应用。
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations
{
if (locations.count > 0) {
// Location manager has nonempty array of readings
// We take the las one - the latest position determined
CLLocation *last = locations[locations.count - 1];
// Evaluate threshold distance to notice change
CLLocationDistance distance = LOCATOR_SENSITIVITY + 1;
if (previous) {
// calculate distance from previous location
distance = [last distanceFromLocation:previous];
}
// update previous location
previous = last;
if (distance > LOCATOR_SENSITIVITY) {
// If we moved far enough, update current position
currentLat = last.coordinate.latitude;
currentLon = last.coordinate.longitude;
// and notify all observers
// You can use delegate here, dispatch_async wrapper etc.
[[NSNotificationCenter defaultCenter]
postNotificationName:LocationIsChanged object:nil];
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.