不知道是老了还是怎么样,脑子里装的东西越来越多了,写过的东西也会忘记。实践强调我,好记性不如烂笔头,做的东西再多,都要多写多记,希望我的内容能对你有帮助
无论swift和oc,其实原理都是一样的
详细步骤如下
1、导入库CoreLocation
2、实例化对象 var locationManager:CLLocationManager = CLLocationManager()
3、继承协议CLLocationManagerDelegate
4、对象属性设置
5、实现协议方法,获取地理信息
4和5如下代码所示
swift
此处注意
locationManager.requestWhenInUseAuthorization()
这句代码是在用户使用app的时候弹出请求框
func reLocationAction(){
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest//定位最佳
locationManager.distanceFilter = 500.0//更新距离
locationManager.requestWhenInUseAuthorization()
if (CLLocationManager.locationServicesEnabled()){
locationManager.startUpdatingLocation()
print("定位开始")
}
}
func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {
LSDKLog("获取经纬度发生错误")
LSDKLog(error)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let thelocations:NSArray = locations as NSArray
let location = thelocations.lastObject as! CLLocation
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
latitudeStr = Float(latitude)
longitudeStr = Float(longitude)
LSDKLog("经纬度")
LSDKLog(latitudeStr)
LSDKLog(longitudeStr)
locationManager.stopUpdatingLocation()
}
OC代码
-(void)getLocation
{
//判断定位功能是否打开
if ([CLLocationManager locationServicesEnabled]) {
locationmanager = [[CLLocationManager alloc]init];
locationmanager.delegate = self;
[locationmanager requestAlwaysAuthorization];
[locationmanager requestWhenInUseAuthorization];
//设置寻址精度
locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
locationmanager.distanceFilter = 50.0;
[locationmanager startUpdatingLocation];
}
}
#pragma mark CoreLocation delegate (定位失败)
//定位失败后调用此代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//设置提示提醒用户打开定位服务
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允许定位提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:okAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark 定位成功后则执行此代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
[locationmanager stopUpdatingHeading];
CLLocation *currentLocation = [locations lastObject];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
//打印当前的经度与纬度
// YEAFNAppLog(@"%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
//反地理编码
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placeMark = placemarks[0];
self.currentCity = placeMark.locality;
if (!self.currentCity) {
self.currentCity = @"无法定位当前城市";
}
/*看需求定义一个全局变量来接收赋值*/
YEAFNAppLog(@"当前国家----%@",placeMark.country);//当前国家
YEAFNAppLog(@"当前的城市%@",self.currentCity);//当前的城市
YEAFNAppLog(@"%@",placeMark.subLocality);//当前的位置
YEAFNAppLog(@"%@",placeMark.thoroughfare);//当前街道
YEAFNAppLog(@"%@",placeMark.name);//具体地址
}
}];
}
网友评论