定位的系统实现简单方法
1、导入头文件:#import <CoreLocation/CoreLocation.h>
2、遵循代理:CLLocationManagerDelegate
3、初始化变量: CLLocationManager *_locationManager;
CLGeocoder *_geocoder;
4、实现代码:
-(void)viewWillAppear:(BOOL)animated { //定位管理器 _locationManager=[[CLLocationManager alloc]init]; _geocoder=[[CLGeocoder alloc]init]; if (![CLLocationManager locationServicesEnabled]) { NSLog(@"定位服务当前可能尚未打开,请设置打开!"); return; } //如果没有授权则请求用户授权 if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){ [_locationManager requestWhenInUseAuthorization]; }else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){ //设置代理 _locationManager.delegate=self; //设置定位精度 _locationManager.desiredAccuracy=kCLLocationAccuracyBest; //定位频率,每隔多少米定位一次 CLLocationDistance distance=10.0;//十米定位一次 _locationManager.distanceFilter=distance; //启动跟踪定位 [_locationManager startUpdatingLocation]; } } #pragma mark - CoreLocation 代理 #pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置) //可以通过模拟器设置一个虚拟位置,否则在模拟器中无法调用此方法 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *location=[locations firstObject];//取出第一个位置 CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标 NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed); [self getAddressByLatitude:coordinate.latitude longitude:coordinate.longitude]; //如果不需要实时定位,使用完即使关闭定位服务 [_locationManager stopUpdatingLocation]; } #pragma mark 根据坐标取得地名 -(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{ //反地理编码 CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude]; [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *placemark=[placemarks firstObject]; NSLog(@"详细信息:%@",placemark.addressDictionary); cityString = [placemark.addressDictionary objectForKey:@"City"]; NSLog(@"%@",cityString); dizhiLable.adjustsFontSizeToFitWidth = YES; dizhiLable.text = cityString; }]; }