简体   繁体   English

对于最新的 iOS 版本 (12.4),不推荐使用 didUpdateLocationsTo: from: 方法。 如何用 didUpdateLocations 替换它?

[英]didUpdateLocationsTo: from: method is deprecated for latest iOS versions(12.4). How to replace it with didUpdateLocations?

I'm trying to implement longitude and latitude in my sample app.我正在尝试在我的示例应用程序中实现经度和纬度。 I use iPhone 5s device to test it.我使用 iPhone 5s 设备进行测试。 It doesn't show any effect on latitude and longitude labels on button actions.它对按钮操作的纬度和经度标签没有任何影响。

Before compiling Xcode shows warning at line didUpdateToLocation: fromLocation: method as在编译 Xcode 之前在 didUpdateToLocation: fromLocation: 方法行显示警告为

"implementing deprecated method" “实施弃用的方法”

please help me with the suitable method which replaces this method for smooth working of the app请帮助我用合适的方法代替此方法以使应用程序顺利运行

With Xcode 12.4 and for iOS 12.3 using objective c I'm trying to implement a simple location demo app which shows longitude and latitude.使用 Xcode 12.4 和 iOS 12.3 使用目标 c 我正在尝试实现一个简单的位置演示应用程序,它显示经度和纬度。 Below are my viewController.h and viewController.m files.下面是我的 viewController.h 和 viewController.m 文件。 And I've tried this below code from online tutorial AppCoda我已经尝试了以下来自在线教程AppCoda的代码

viewController.h视图控制器.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视图控制器.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

在此处输入图像描述

On clicking button get my location nothing is happening.单击按钮获取我的位置没有任何事情发生。 It should actually update the co-ordinates besides Longitude: and Latitude: some float values..but it's not doing that..I'll ask please suggest me links to any tutorial web site or any GitHub project for location handling in iOS 12.3 and above in objective c...Thanks for the replies and suggested edits.它实际上应该更新除经度之外的坐标:和纬度:一些浮点值..但它没有这样做..我会问请建议我链接到任何教程 web 站点或任何 GitHub 项目中的位置处理和 Z1BDF605039194CDB11以上目标 c...感谢您的回复和建议的编辑。

As the docs say, just use locationManager(_:didUpdateLocations:) .正如文档所说,只需使用locationManager(_:didUpdateLocations:)

Swift 5 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 Objective-C

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    [manager stopUpdatingLocation];

    CLLocation *newLocation = [locations lastObject];
    if (newLocation) {
        <... Do something with the location ...>
    }
}

I hope this implementation for delegate method will help you.我希望这个委托方法的实现会对你有所帮助。 works both on Mac and IOS.适用于 Mac 和 IOS。 I used it to pinpoint current position for weather application.我用它来确定当前的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM