CLLocationManager的startUpdatingLocation不工作
来源:互联网 发布:淘宝店铺logo制作教程 编辑:程序博客网 时间:2024/06/04 18:44
如何知道iphone的物理位置,这一问题在LBS的应用中很常见。解决起来其实也很简单,就两步。第一步,通过CoreLocation获取经度纬度;第二步,通过google网络服务或者其他架构及获取经纬度以得到物理地址。
获取经度纬度的操作过程
首先,
添加CoreLocation.Frameworks,并将其头文件放在需要引用的类的头文件中,且在头文件中声明一个CLLocationManager类的属性对象。在头文件中@interface后加上<CLLocationManagerDelegate>;
其次,
在头文件中声明属性locationManager;
@property (nonatomic, weak) CLLocationManager *locationManager;
因为XCODE使用的是4.5版本,所以在implement文件中可以不加@synchronized locationManager;
最后,在实现文件中实现CLLocationManagerDelegate的必须实现的方法和CLLocationManager调用。
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
[self.locationManager startUpdatingLocation];
}
委托CLLocationManagerDelegate中两个方法需要实现。
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
DebugLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
DebugLog(@"%@",error);
}
但是,在调试过程中,发现委托的方法并没有执行。在属性locationManager经过编译器设置的_locationManager已经在viewDidLoad执行结束后就释放了,所以,委托方法是不可能执行的。
两个解决方法,其一,调整属性设置,@property (nonatomic, strong) CLLocationManager *locationManager;其二,不要属性设置,自己在interface()中定义实例变量,再自己初始化。
因为默认对象的引用指针保留方式都是strong。
获取物理位置的操作过程
我们已经获取了经纬度,要知道物理位置,到经纬度与物理位置对应的数据库中去找即可。
获取经度纬度的操作过程
首先,
添加CoreLocation.Frameworks,并将其头文件放在需要引用的类的头文件中,且在头文件中声明一个CLLocationManager类的属性对象。在头文件中@interface后加上<CLLocationManagerDelegate>;
其次,
在头文件中声明属性locationManager;
@property (nonatomic, weak) CLLocationManager *locationManager;
因为XCODE使用的是4.5版本,所以在implement文件中可以不加@synchronized locationManager;
最后,在实现文件中实现CLLocationManagerDelegate的必须实现的方法和CLLocationManager调用。
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
[self.locationManager startUpdatingLocation];
}
委托CLLocationManagerDelegate中两个方法需要实现。
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
DebugLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
DebugLog(@"%@",error);
}
但是,在调试过程中,发现委托的方法并没有执行。在属性locationManager经过编译器设置的_locationManager已经在viewDidLoad执行结束后就释放了,所以,委托方法是不可能执行的。
两个解决方法,其一,调整属性设置,@property (nonatomic, strong) CLLocationManager *locationManager;其二,不要属性设置,自己在interface()中定义实例变量,再自己初始化。
因为默认对象的引用指针保留方式都是strong。
获取物理位置的操作过程
我们已经获取了经纬度,要知道物理位置,到经纬度与物理位置对应的数据库中去找即可。
方法很多中,通过google可以,http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true。但是google不幸被墙了,只能通过其他方式,有人说高德可以,也有人说第三方框架也可以。
On iOS 5 MKReverseGeoCoder is Deprecated!
So you want to use CLGeocoder with CLLocationManager, very simple and works with block.
Example:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [self.locationManager stopUpdatingLocation]; CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark * placemark in placemarks) { .... = [placemark locality]; } }];}
- CLLocationManager的startUpdatingLocation不工作
- CLLocationManager 关于地图 模拟器运行不提示授权的解决方法(即不执行代理方法)
- CLLocationManager
- ios8用CLLocationManager定位获取经纬度,不执行代理的方法的解决办法
- iOS8中定位服务的变化 CLLocationManager协议方法不响应
- iOS8使用CLLocationManager定位时,代理方法不执行的解决方法
- CLLocationManager定位坐标不准确问题以及WGS_84转GCJ_02坐标位置纠错的方法
- CLLocationManager定位失败后不提示?
- CLLocationManager的相关属性讲解
- 地图之CLLocationManager的使用
- 地图之CLLocationManager的使用
- 地图之CLLocationManager的使用
- 地图之CLLocationManager的使用
- 地图之CLLocationManager的使用
- 地图之CLLocationManager的使用
- 地图之CLLocationManager的使用
- 不工作的日子
- 工作的不开心
- android 通过GPS获取用户地理位置并监听位置变化
- WebService调用一对多关联关系时引起问题:A cycle is detected in the object graph
- linux下动态库so文件的一些认识 和 gcc编译的几个阶段
- 删除所有的.svn文件夹
- mysql模拟生成id,弥补没有oracle中rownum的遗憾
- CLLocationManager的startUpdatingLocation不工作
- oracle sql 5
- 数组去重复
- JAVA操作COOKIE 收藏
- 当MFC中出现 尚未调用CoInitialize
- 本来决定好了的事情又改变主意了,纠结纠结... ...
- oralce sql 6
- VB.NET BRUSHES 色卡(色表)
- C#禁止双击标题栏