gpt4 book ai didi

ios - CLLocationManager 委托(delegate)方法 didUpdateToLocation 未被调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:16:33 28 4
gpt4 key购买 nike

我有以下代码来获取我的纬度、经度、水平精度和高度。但是这个方法没有被调用,

-(void)locationManager:(CLLocationManager *)manage 
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;

我的代码,

//
// CDViewController.m
// Compass
//
// Created by Naveen Kumar on 09/04/14.
// Copyright (c) 2014 CamDon. All rights reserved.
//

#import "CDViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface CDViewController ()

@end

@implementation CDViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.currentHeading = [[CLHeading alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.headingFilter = 1;
self.locationManager.delegate = self;
[self.locationManager startUpdatingHeading];
NSLog(@"%f",self.locationManager.location.coordinate.latitude);
NSLog(@"%f",self.locationManager.location.coordinate.longitude);
_startLocation = nil;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
self.currentHeading = newHeading;


self.headingLabel.text = [NSString stringWithFormat:@"%d°", (int)newHeading.magneticHeading];
}

- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
{
if(self.currentHeading == nil)
{
return YES;
}
else
{
return NO;
}
}
#pragma mark -
#pragma mark CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.coordinate.latitude];
_latitude.text = currentLatitude;

NSString *currentLongitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.coordinate.longitude];
_longitude.text = currentLongitude;

NSString *currentHorizontalAccuracy =
[[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.horizontalAccuracy];
_horizontalAccuracy.text = currentHorizontalAccuracy;

NSString *currentAltitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.altitude];
_altitude.text = currentAltitude;

NSString *currentVerticalAccuracy =
[[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.verticalAccuracy];
_verticalAccuracy.text = currentVerticalAccuracy;

if (_startLocation == nil)
_startLocation = newLocation;

CLLocationDistance distanceBetween = [newLocation
distanceFromLocation:_startLocation];

NSString *tripString = [[NSString alloc]
initWithFormat:@"%f",
distanceBetween];
_distance.text = tripString;
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
}
@end

我的 .h 文件

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

@interface CDViewController : UIViewController<UIApplicationDelegate,CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *headingLabel;
@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLHeading * currentHeading;
@property (strong, nonatomic) IBOutlet UILabel *latitude;
@property (strong, nonatomic) IBOutlet UILabel *longitude;
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *altitude;
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *distance;
@property (strong, nonatomic) CLLocation *startLocation;
@end

viewDidLoad 方法中 NSLog 的输出是 0.0000,0.0000

locationManager:didUpdateToLocation:fromLocation: 委托(delegate)方法未被调用的原因可能是什么?

最佳答案

您只调用了 [self.locationManager startUpdatingHeading];,因此您将获得对 locationManager:didUpdateHeading: 的回调。

如果您想要回调到 locationManager:didUpdateLocations:,您应该在位置管理器上调用 startUpdatingLocation

关于ios - CLLocationManager 委托(delegate)方法 didUpdateToLocation 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22958946/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com