iOS 手机定位 获取当前城市
2016-03-10 17:28
1611 查看
先导入头文件
#import <CoreLocation/CoreLocation.h>
我之前写的 一直没有走定位的代理方法 然后最后找到 原因是在 Info.plist文件中加
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription 都设置为Boolean 值都为YES
@property (nonatomic,strong)CLLocationManager *locationManager;
@property (nonatomic,copy)NSString *cityName;
#pragma mark 定位
- (void)locate
{
if (!self.locationManager)
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter=1.0;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
} else {
[self.locationManager startUpdatingLocation];
}
}
#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];
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
self.cityName = city;
[manager stopUpdatingLocation];
}
else if (error == nil && [array count] == 0)
{
self.cityName=@"定位失败";
[self judgeLocationIsTure];
}
else if (error != nil)
{
self.cityName=@"定位失败";
}
}];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
if (error.code == kCLErrorDenied) {
self.cityName=@"定位失败";
// 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
}
#import <CoreLocation/CoreLocation.h>
我之前写的 一直没有走定位的代理方法 然后最后找到 原因是在 Info.plist文件中加
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription 都设置为Boolean 值都为YES
@property (nonatomic,strong)CLLocationManager *locationManager;
@property (nonatomic,copy)NSString *cityName;
#pragma mark 定位
- (void)locate
{
if (!self.locationManager)
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter=1.0;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
} else {
[self.locationManager startUpdatingLocation];
}
}
#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];
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
self.cityName = city;
[manager stopUpdatingLocation];
}
else if (error == nil && [array count] == 0)
{
self.cityName=@"定位失败";
[self judgeLocationIsTure];
}
else if (error != nil)
{
self.cityName=@"定位失败";
}
}];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
if (error.code == kCLErrorDenied) {
self.cityName=@"定位失败";
// 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
}
相关文章推荐
- iOS7光标问题
- ios桥接 bridge
- iOS 版本怎么更新?
- iOS 动画详解
- kidd风的IOS日志之IOS9获取联系人信息Contact FrameWork的基本使用
- iOS----- Crash 分析(文三)- 符号化崩溃日志
- iOS----- Crash 分析(文二)-崩溃日志组成
- ios 'Can't add self as subview'崩溃日志详解
- ios的toolbar的item的几种简单布局
- ios 判断某个时间是周几的方法
- ios 判断当前时间是否在某个时间段的方法
- iOS多线程简介
- iOS --------Crash 分析(一)
- iOS 关于Block的使用及循环引用
- 动画的执行--ios动画
- iOS-git命令使用教程
- ios-NSString与NSDate互相转换及时间早晚比较
- iOS-----dSYM 文件分析工具
- iOS开发之再议事件的产生和传递
- iOS9使用提示框的正确实现方式