【发布时间】: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