gpt4 book ai didi

ios - didUpdateToLocation 未在应用程序中调用

转载 作者:行者123 更新时间:2023-11-29 03:08:57 24 4
gpt4 key购买 nike

我想接收位置更新。我已经在头文件中添加了一个位置委托(delegate),但是 didUpdateToLocation 方法没有触发我的代码是

@interface FirstViewController : UIViewController <CLLocationManagerDelegate>
{
UILabel *myLabel;
CLLocationManager *manager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
-(void) showCurrentLocation;
@end

和 .m 文件是这样的:

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController


-(id) init
{

self=[super init];
if (self) {
self.view.backgroundColor=[UIColor purpleColor];
self.edgesForExtendedLayout=UIRectEdgeNone;

[self showCurrentLocation];
}
return self;
}


-(void) showCurrentLocation{

manager=[[CLLocationManager alloc] init];
manager.pausesLocationUpdatesAutomatically=NO;

geocoder=[[CLGeocoder alloc] init];

manager.delegate=self;
manager.desiredAccuracy=kCLLocationAccuracyBest;
}

-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{

NSLog(@"Error %@",error);
NSLog(@"Faild to get location");
}

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location %@",newLocation);
CLLocation *currentlocation=newLocation;

if (currentlocation!=nil)
{
myLabel=[[UILabel alloc] initWithFrame:CGRectMake(30, 30, 200, 200)];
[myLabel setText:[NSString stringWithFormat:@"Latit %8f",currentlocation.coordinate.latitude]];
myLabel.textColor=[UIColor whiteColor];
[self.view addSubview:myLabel];

[geocoder reverseGeocodeLocation:currentlocation completionHandler:^(NSArray *placemark1 ,NSError *erroe){

if (erroe==nil && [placemark1 count]>0)
{
NSLog(@"Location");
}
else
{
NSLog(@"error 2 %@",erroe.debugDescription);
}
}];
}
}

最佳答案

您需要启动位置管理器

    manager = [CLLocationManager new];        
manager.delegate = self;

[manager startUpdatingLocation]; // forgotten in the code above

还要确保您正确设置方案以通过模拟器在测试模式下模拟位置

enter image description here

关于ios - didUpdateToLocation 未在应用程序中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22442671/

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