IOS 学习(基于 iOS 8.0 以上的地理信息查询)
来源:互联网 发布:电脑重装系统恢复数据 编辑:程序博客网 时间:2024/06/05 15:57
当 iOS 版本 大于 8.0 时 添加两个 配置信息
要利用CoreLocation,必须在frameworks里面加入“CoreLocation.framework”。在最新版本的Xcode中加入新的framework步骤如下:
单击项目的target =>在出来的xcodeproj面板中点击“Link Binary With Libraries” =>点击“+”,然后选择需要的framework即可。
在info.plist中添加
-NSLocationAlwaysUsageDescription Bealoon YES-NSLocationWhenInUseUsageDescription Bealoon YES 调用 requestAlwaysAuthorization 方法/*When the current authorization status is kCLAuthorizationStatusNotDetermined, this method runs asynchronously and prompts the user to grant permission to the app to use location services. The user prompt contains the text from the NSLocationWhenInUseUsageDescription key in your app’s Info.plist file, and the presence of that key is required when calling this method. After the status is determined, the location manager delivers the results to the delegate’s locationManager:didChangeAuthorizationStatus: method. If the current authorization status is anything other than kCLAuthorizationStatusNotDetermined, this method does nothing and does not call the locationManager:didChangeAuthorizationStatus: method.You must call this method or the requestAlwaysAuthorization method prior to using location services. If the user grants “when-in-use” authorization to your app, your app can start most (but not all) location services while it is in the foreground. (Apps cannot use any services that automatically relaunch the app, such as region monitoring or the significant location change service.) When started in the foreground, services continue to run in the background if your app has enabled background location updates in the Capabilities tab of your Xcode project. Attempts to start location services while your app is running in the background will fail. The system displays a location-services indicator in the status bar when your app moves to the background with active location services.For more information about the NSLocationWhenInUseUsageDescription key, see Information Property List Key Reference./或者 requestWhenInUseAuthorization 方法/*When the current authorization status is kCLAuthorizationStatusNotDetermined, this method runs asynchronously and prompts the user to grant permission to the app to use location services. The user prompt contains the text from the NSLocationAlwaysUsageDescription key in your app’s Info.plist file, and the presence of that key is required when calling this method. After the status is determined, the location manager delivers the results to the delegate’s locationManager:didChangeAuthorizationStatus: method. If the current authorization status is anything other than kCLAuthorizationStatusNotDetermined, this method does nothing and does not call the locationManager:didChangeAuthorizationStatus: method.Important:ImportantRequesting “Always” authorization is discouraged because of the potential negative impacts to user privacy. You should request this level of authorization only when doing so offers a genuine benefit to the user.You must call this method or the requestWhenInUseAuthorization method prior to using location services. When the user grants “Always” authorization to your app, your app can start any of the available location services while your app is running in the foreground or background. In addition, services that allow your app to be launched in the background continue to do so.For more information about the NSLocationAlwaysUsageDescription key, see Information Property List Key Reference.*/
ViewControl.h
#import <UIKit/UIKit.h>#include <CoreLocation/CoreLocation.h>#include <CoreLocation/CLLocationManagerDelegate.h>@interface ViewController : UIViewController<CLLocationManagerDelegate>@property (weak, nonatomic) IBOutlet UITextField *cityName;@property (weak, nonatomic) IBOutlet UITextField *txtLat;@property (weak, nonatomic) IBOutlet UITextField *txtLon;@property (weak, nonatomic) IBOutlet UITextField *txtAlt;- (IBAction)showLocationInformation:(id)sender;@property (nonatomic, strong) CLLocationManager *locationManager;@end
ViewControl.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _locationManager = [[CLLocationManager alloc]init]; _locationManager.delegate = self; /*值得注意的是,一定要设置locationManager的delegate是类本身,这样startUpdatingLocation运行的时候才会调用第二步实现的方法。 如果机器没有开启地理位置的服务,那么就不需要做多余的动作。*/ if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0) { NSLog(@"ios 8.0"); [self.locationManager requestAlwaysAuthorization];// 前后台同时定位 }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (IBAction)showLocationInformation:(id)sender { _locationManager.desiredAccuracy = kCLLocationAccuracyBest//设置精度 _locationManager.distanceFilter = 1000.0f;//设置需要更新时的移动距离 [_locationManager startUpdatingLocation];}-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ CLLocation *location = [locations lastObject]; //_cityName.text = [location cur] _txtLat.text = [NSString stringWithFormat:@"%3.5f",location.coordinate.latitude]; _txtLon.text = [NSString stringWithFormat:@"%3.5f", location.coordinate.longitude]; _txtAlt.text = [NSString stringWithFormat:@"%3.5f", location.altitude]; CLGeocoder *geocoder = [[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray * array, NSError *error){ if (array.count > 0) { CLPlacemark *placemark = [array objectAtIndex:0]; // NSLog(@"%@", placemark); NSString *cityName = [placemark locality]; if (!cityName) { cityName = [placemark administrativeArea]; } self.cityName.text = cityName; } else if (error == nil && [array count] == 0) { NSLog(@"No results were returned."); } else if (error != nil) { NSLog(@"An error occurred = %@", error); } }]; [_locationManager stopUpdatingLocation];}@end
0 0
- IOS 学习(基于 iOS 8.0 以上的地理信息查询)
- iOS UISearchController的使用(iOS 8.0以上)
- ios 地理信息反编码
- iOS菜鸟学习——获得当前位置及地理信息
- IOS学习之——定位服务3 地理信息编码
- iOS 基于FMDB简单的查询操作
- iOS 地图(Map)与地理信息反编码
- iOS中基于BLE的学习博客
- [iOS]毛玻璃在iOS8以上和iOS7以上的实现
- IOS 3.1以上 所支持的字体
- ios 3.0 以上支持的字体
- iOS开发——设置支持的iOS设备(512m内存以上设备)
- iOS开发——设置支持的iOS设备(512m内存以上设备)
- 根据IP查询地理信息的工具类
- iOS学习之iOS5.0以上 使用新…
- iOS学习之iOS5.0以上使用新…
- iOS学习之iOS5.0以上 …
- iOS的查询、过滤(NSPredicate)
- iOS 四种保存数据的方式 (github上面的)
- Java基于socket编程
- 问题积累
- Linux下安装Tomcat(tar.gz)
- Android性能优化典范(二)
- IOS 学习(基于 iOS 8.0 以上的地理信息查询)
- COCOS学习笔记--自定义绘制
- 内存映射
- iOS - 调用相机相册的方法 + 上传图片
- mac上切换jdk版本
- 使用Maven自动部署Java Web项目到Tomcat问题小记
- easyui合并行(同列内容合并)
- tomcat在管理界面部署项目,设置管理用户
- iOS - 检测网络状态: Reachability和非Reachability