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
- iOS-CoreLocation实现定位当前城市
- iOS-CoreLocation实现定位当前城市
- iOS CoreLocation 实现简单定位(现实城市)
- iOS定位-利用CoreLocation.framework获取当前城市
- ios 定位当前城市
- iOS 定位当前城市
- ios定位当前城市
- iOS开发 -- CoreLocation实现定位
- iOS 定位 <CoreLocation/CoreLocation.h>
- iOS开发 CoreLocation实现定位服务
- iOS定位服务CoreLocation
- iOS定位--CoreLocation
- iOS 使用CoreLocation定位
- CoreLocation IOS 定位详解
- iOS-使用CoreLocation定位
- iOS定位服务 CoreLocation
- iOS CoreLocation框架 定位
- iOS 地图CoreLocation定位
- document.close()
- sicily 10571 MISA
- 子查询的分类
- 最短路小结
- Graph Cut and Its Application in Computer Vision
- iOS-CoreLocation实现定位当前城市
- 【leetcode】:move zeros
- ActivityGroup
- awr
- XCode 7.1 安装 Alcatraz包管理器失败的处理
- HBase基本概念和hbase shell常用命令用法
- ZendStudio调试配置(XDebug)
- C++中map的排序问题
- 欢迎使用CSDN-markdown编辑器