iOS-CoreLocation实现定位当前城市

来源:互联网 发布:战舰世界mac 国服 编辑:程序博客网 时间:2024/05/21 10:18

       我们可能常常使用CoreLocation来实现很复杂的功能,包括地图显示、定位等等。但是在实际的项目开发中,我们可能有这样的需求,只要获取当前所在城市的名称即可,然后使用这个城市名称来开发其他的功能实现,并不需要地图等太复杂的功能。这个Demo可以直接作为一个单独的模块加入到你的项目中,非常方便。我已经上传至 https://github.com/chenyufeng1991/LocationCity  。实现代码如下:

(1)因为涉及网络操作和定位功能,所以我们需要在Info.plist文件中加入如下字段:其中NSAppTransportSecurity和NSAllowArbitryLoads是由于Xcode7修改了关于网络访问的安全性而需要加入的。


(2)首先需要加入CoreLocation框架,然后由于我使用一个对话框弹出来显示当前城市,所以我把Main.storyboard也删除了。这样逻辑结构更为清晰。

(3)在AppDelegate.m中实现如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    ViewController * viewController = [[ViewController alloc] init];    [_window setRootViewController:viewController];    [_window makeKeyAndVisible];        return YES;}

(4)在ViewController中实现如下:

#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate>@property (nonatomic, strong) CLLocationManager* locationManager;@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];    //检测定位功能是否开启  if([CLLocationManager locationServicesEnabled]){        if(!_locationManager){            self.locationManager = [[CLLocationManager alloc] init];            if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){        [self.locationManager requestWhenInUseAuthorization];        [self.locationManager requestAlwaysAuthorization];              }            //设置代理      [self.locationManager setDelegate:self];      //设置定位精度      [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];      //设置距离筛选      [self.locationManager setDistanceFilter:100];      //开始定位      [self.locationManager startUpdatingLocation];      //设置开始识别方向      [self.locationManager startUpdatingHeading];          }      }else{    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil                                                         message:@"您没有开启定位功能"                                                        delegate:nil                                               cancelButtonTitle:@"确定"                                               otherButtonTitles:nil, nil];    [alertView show];  }}#pragma mark - CLLocationManangerDelegate//定位成功以后调用- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {    [self.locationManager stopUpdatingLocation];  CLLocation* location = locations.lastObject;  [self reverseGeocoder:location];}#pragma mark Geocoder//反地理编码- (void)reverseGeocoder:(CLLocation *)currentLocation {    CLGeocoder* geocoder = [[CLGeocoder alloc] init];  [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {        if(error || placemarks.count == 0){      NSLog(@"error = %@",error);    }else{            CLPlacemark* placemark = placemarks.firstObject;      NSLog(@"placemark:%@",[[placemark addressDictionary] objectForKey:@"City"]);      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你的位置" message:[[placemark addressDictionary] objectForKey:@"City"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];            [alert show];          }      }];}@end

(5)最后的实现效果如下:



github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 胖孩子脖子黑怎么办 脖子后面太黑怎么办 脸白脖子黑怎么办 脖子后面很黑怎么办 打印一个字一页怎么办 小黄车没锁被别人骑走了怎么办 幼儿早上不早起怎么办 工作中不细心怎么办 小孩没时间观念怎么办 高中厌学想回家怎么办 一年级孩子不喜欢数学怎么办 孩子做事不细心怎么办 孩子学习不够细心怎么办 孩子不用心学习怎么办? 恋爱中不够细心怎么办 孩子不愿学英语怎么办 孩子不愿学数学怎么办 孩子不愿学钢琴怎么办 小孩子学习不开窍怎么办 一年级没有读好怎么办 小孩眉毛很杂乱怎么办 孩子不好好听课怎么办 成绩差的孩子怎么办 三年级语文太差怎么办? 孩子叛逆不学习怎么办 三年级阅读理解能力差怎么办 孩子静不下心怎么办 孩子体温低于35怎么办 小孩体温突然低怎么办 宝宝35.5度体温怎么办 宝宝出汗体温低怎么办 小孩体温35.2度怎么办 孩子睡觉出冷汗怎么办 小孩感冒发烧咳嗽怎么办 小孩咳嗽出汗多怎么办 小孩手脚出汗多怎么办 小孩感冒出虚汗怎么办 宝宝感冒出冷汗怎么办 宝宝感冒睡觉出汗怎么办 宝宝感冒冒冷汗怎么办 宝宝感冒爱出汗怎么办