iOS 定位于地理反编码
2024-08-27 23:40:51
- (void)viewDidLoad { [self startLocation];
} //开始定位
-(void)startLocation{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; [self.locationManager requestWhenInUseAuthorization];
if ([[[UIDevice currentDevice]systemVersion]doubleValue]>=8.0) { [self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization]; }
[self.locationManager startUpdatingLocation]; if ([CLLocationManager locationServicesEnabled]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
}else
{
NSLog(@"open fail");
} }
#pragma mark - CoreLocation 代理
#pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location=[locations lastObject];
CLLocationCoordinate2D coordinate=location.coordinate;
NSLog(@"经度:%f,纬度:%f",coordinate.longitude,coordinate.latitude);
self.longitude=coordinate.longitude;
self.latitude=coordinate.latitude;
//如果不需要实时定位,使用完即使关闭定位服务
[self.locationManager stopUpdatingLocation];
[self weiZhi];
}
-(void)weiZhi
{
self.geocoder=[[CLGeocoder alloc]init];
CLLocation *rr=[[CLLocation alloc]initWithLatitude:self.latitude longitude:self.longitude];
[self.geocoder reverseGeocodeLocation:rr completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *mark=[placemarks objectAtIndex:];
UILabel *currentLocation=[[UILabel alloc]initWithFrame:CGRectMake(, , , )];
currentLocation.text=[NSString stringWithFormat:@"%@",mark.subLocality]; [self.locationView addSubview:currentLocation];
}]; }
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
break;
default:
break;
} }
//当定位出现错误时就会调用这个方法。
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"error-%@",error); }
定位当前的位置。对于 CLPlacemark 还有许多属性,不仅仅可以给出当前位置,还可以给出其他信息,后续研究。。。。
最新文章
- Owin:“System.Reflection.TargetInvocationException”类型的未经处理的异常在 mscorlib.dll 中发生
- 《Java编程思想》学习笔记(二)——类加载及执行顺序
- linux lin命令
- arcgis engine 监听element的添加、更新和删除事件(使用IGraphicsContainerEvents)
- SQL INSERT INTO 语句
- [转载] 高流量大并发Linux TCP 性能调优
- STM32F4_USART配置及细节描述
- L001-老男孩教育-Python13期VIP视频-19节-pbb
- (转)Call to undefined function mb_convert_encoding()
- JS(六)
- poj3273 二分
- crm查询记录共享给了哪些人
- 使用 gridfs-stream 存储文件遇到的一个坑。
- android官方技术文档翻译——Android Lint
- python3+selenium框架设计07-unittest单元测试框架
- 大数据小白系列——MR(1)
- web机试
- hdu1686 Oulipo KMP/AC自动机
- https知识了解与javaweb中如何使用https
- FairyGUI学习
热门文章
- 使用spawn-fcgi管理php-cgi
- 【接口测试】Jenkins+Ant+Jmeter搭建持续集成的接口测试平台
- C# 检测程序运行时间的方法,Stopwatch类
- Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]
- ModalPopup
- poj1192 最优连通子集(树形dp)
- BZOJ4049 [Cerc2014] Mountainous landscape
- SunSonic 3.0 ORM开源框架的学习
- C语言学习常识
- 跑马灯标记marquee