【问题标题】:didUpdateToLocation is not called in app应用程序中未调用 didUpdateToLocation
【发布时间】:2014-04-21 23:13:50
【问题描述】:

我想接收位置更新。我已经在头文件中添加了一个位置委托,但是 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);
            }
        }];
    }
}

【问题讨论】:

  • 添加“位置委托到头文件”只是告诉编译器这个类打算为期望该委托的另一个类的 some 实例实现指定的协议。除了按照答案所说的去做,确保 init 确实被 VC 调用了。

标签: ios objective-c cllocationmanager


【解决方案1】:

您需要启动位置管理器

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

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

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

【讨论】:

  • OP 正在设置delegate
  • 操作?你的意思是?问题的核心是没有startUpdatingLocation
  • "OP" - 原始海报。我的意思是您声明需要设置委托。它已设置。您关于致电startUpdatingLocation 的观点是正确的。只需删除有关需要设置委托的部分,因为它已经完成了。
猜你喜欢
  • 2018-03-29
  • 2014-11-30
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多