mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1551 随笔 :: 0 文章 :: 14 评论 :: 123万 阅读
1.倒入头文件

#import <CoreLocation/CoreLocation.h>

2.实现定位协议CLLocationManagerDelegate

3.定义定位属性

@property(nonatomic,retain)CLLocationManager *locationManager;

4.開始定位

- (void)locate

{

    // 推断定位操作是否被同意

    if([CLLocationManager locationServicesEnabled]) {

        self.locationManager = [[CLLocationManager alloc] init] ;

        

        self.locationManager.delegate = self;

    }else {

        //提示用户无法进行定位操作

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:

        @"提示" message:@"定位不成功 ,请确认开启定位" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

        [alertView show];

    }

    // 開始定位

    [self.locationManager startUpdatingLocation];

}


5.实现定位协议回调方法

#pragma mark - CoreLocation Delegate

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

    //此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,假设不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation

    CLLocation *currentLocation = [locations lastObject];

    

    // 获取当前所在的城市名

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    //依据经纬度反向地理编译出地址信息

    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)

     {

         if (array.count > 0)

         {

             CLPlacemark *placemark = [array objectAtIndex:0];

             

             //将获得的全部信息显示到label

             NSLog(@"%@",placemark.name);

             //获取城市

             NSString *city = placemark.locality;

             if (!city) {

                 //四大直辖市的城市信息无法通过locality获得,仅仅能通过获取省份的方法来获得(假设city为空,则可知为直辖市)

                 city = placemark.administrativeArea;

             }

             self.cityName = city;

         }

         else if (error == nil && [array count] == 0)

         {

             NSLog(@"No results were returned.");

         }

         else if (error != nil)

         {

             NSLog(@"An error occurred = %@", error);

         }

     }];

    //系统会一直更新数据。直到选择停止更新。由于我们仅仅须要获得一次经纬度就可以,所以获取之后就停止更新

    [manager stopUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error {

    

    if (error.code == kCLErrorDenied) {

        // 提示用户出错原因。可按住Option键点击 KCLErrorDenied的查看很多其它出错信息,可打印error.code值查找原因所在

    }

}



posted on   mthoutai  阅读(845)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示