在iOS8以前,app第一次開始定位服務時,系統會彈出一個提示框來讓用戶選擇是否容許使用定位信息。但iOS8後,app將不會出現這個彈窗。第一次運行以後,在設置->隱私->定位服務中,你的app沒有任何設置,既不是「永不」,也不是「始終」。php
IOS8新增Key:NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription,這兩個Key的值將分別用於描述應用程序始終使用和使用期間使用定位的說明,這些說明將顯示在用戶設置中。 info新增鍵值對以下: html
NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription的值,這個值會顯示在系統提示框中。ios
兩個KEY至少有一個,後面的描述能夠不寫或本身隨意寫。git
這兩個字段沒什麼特別的意思,就是自定義提示用戶受權使用地理定位功能時的提示語。app
對應的方法: 在初使化_locationManager 時使用一個(iOS8CLLocationManager新增實例方法)post
[_locationManager requestWhenInUseAuthorization];//使用時打開ui
[_locationManager requestAlwaysAuthorization];//始終打開編碼
[_locationManager startUpdatingLocation];
atom
#import <CoreLocation/CoreLocation.h>url
@interface nextViewController : UIViewController<CLLocationManagerDelegate>
@property(nonatomic, strong) CLLocationManager *locationManager;
@property CLLocation *currLocation ;
@property CLGeocoder *geocoder;
- (void) location{
_locationManager= [[CLLocationManager alloc]init];
_locationManager.delegate = self;
[_locationManager requestAlwaysAuthorization];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = kCLDistanceFilterNone;
[_locationManager requestWhenInUseAuthorization]; //ios8 新增方法
[_locationManager startUpdatingLocation];
}
#pragma mark - CoreLocation 代理
#pragma mark 跟蹤定位代理方法,每次位置發生變化即會執行(只要定位到相應位置)
//能夠經過模擬器設置一個虛擬位置,不然在模擬器中沒法調用此方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location=[locations firstObject];//取出第一個位置
CLLocationCoordinate2D coordinate=location.coordinate;//位置座標
NSLog(@"經度:%f,緯度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
//若是不須要實時定位,使用完即便關閉定位服務
[_locationManager stopUpdatingLocation];
_currLocation = location; //全局location 用於反理理編碼
[self reverseGeocode1];
}
//失敗 報錯
-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error:%@",error);
}
//另外一個經常使用的代理
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusNotDetermined:
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
}
break;
default:
break;
}
}
- (void)reverseGeocode1{
// 根據座標取得地名
NSString *longtitudeText = @"114.10";
NSString *latitudeText = @"28.19";
CLLocationDegrees latitude = [latitudeText doubleValue];
CLLocationDegrees longitude = [longtitudeText doubleValue];
//注意初使化location參數 double轉換,這裏犯過錯, 此外通常會用 全局_currLocation,在定位後獲得
CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark=[placemarks firstObject];
NSLog(@"詳細信息:%@",placemark.addressDictionary);
}];
}
#pragma mark 地理編碼 根據地名肯定地理座標
-(void)getCoordinateByAddress:(NSString *)address{
//地理編碼
[_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
//取得第一個地標,地標中存儲了詳細的地址信息,注意:一個地名可能搜索出多個地址
CLPlacemark *placemark=[placemarks firstObject];
CLLocation *location=placemark.location;//位置
CLRegion *region=placemark.region;//區域
NSDictionary *addressDic= placemark.addressDictionary;//詳細地址信息字典,包含如下部分信息
// NSString *name=placemark.name;//地名
// NSString *thoroughfare=placemark.thoroughfare;//街道
// NSString *subThoroughfare=placemark.subThoroughfare; //街道相關信息,例如門牌等
// NSString *locality=placemark.locality; // 城市
// NSString *subLocality=placemark.subLocality; // 城市相關信息,例如標誌性建
// NSString *administrativeArea=placemark.administrativeArea; // 州
// NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其餘行政區域信
// NSString *postalCode=placemark.postalCode; //郵編
// NSString *ISOcountryCode=placemark.ISOcountryCode; //國家編碼
// NSString *country=placemark.country; //國家
// NSString *inlandWater=placemark.inlandWater; //水源、湖泊
// NSString *ocean=placemark.ocean; // 海洋
// NSArray *areasOfInterest=placemark.areasOfInterest; //關聯的或利益相關的地標
NSLog(@"位置:%@,區域:%@,詳細信息22222222:%@",location,region,addressDic);
}];
}
詳細的定位說時,能夠看:http://www.cnblogs.com/kenshincui/p/4125570.html