1、導(dǎo)入框架
CoreLocation.framework
2吠昭、添加頭文件
#import <CoreLocation/CoreLocation.h>
3喊括、聲明和代理
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong)CLLocationManager *locationManager;
@end
4、初始化
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = kCLDistanceFilterNone;
5矢棚、開始定位
- (void)startLocation {
if (IOS8) {
// 由于iOS8中定位的授權(quán)機制改變, 需要進行手動授權(quán)
[_locationManager requestAlwaysAuthorization];
[_locationManager requestWhenInUseAuthorization];
}
[_locationManager startUpdatingLocation];
}
6郑什、代理回調(diào)
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@",[NSString stringWithFormat:@"經(jīng)度:%3.5f\n緯度:%3.5f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]);
// 獲取當(dāng)前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根據(jù)經(jīng)緯度反向地理編譯出地址信息
[geocoder reverseGeocodeLocation:newLocation 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;
}
NSLog(@"city = %@", city);
NSLog(@"dic = %@",placemark.addressDictionary);
} else if (error == nil && [array count] == 0) {
NSLog(@"No results were returned.");
} else if (error != nil) {
NSLog(@"An error occurred = %@", error);
}
}];
//系統(tǒng)會一直更新數(shù)據(jù)蘑拯,直到選擇停止更新,因為我們只需要獲得一次經(jīng)緯度即可兜粘,所以獲取之后就停止更新
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
[manager stopUpdatingLocation];
NSString *errorString;
NSString *alterTitle;
NSLog(@"Error: %@",[error localizedDescription]);
switch([error code]) {
case kCLErrorDenied:
//Access denied by user
errorString = @"請在系統(tǒng)設(shè)置中開啟定位服務(wù)\n(設(shè)置>隱私>定位服務(wù))";
alterTitle = @"定位服務(wù)未開啟";
break;
case kCLErrorNetwork:
//Probably temporary...
errorString = @"網(wǎng)絡(luò)未開啟,請檢查網(wǎng)絡(luò)設(shè)置";
alterTitle = @"提示";
break;
default:
errorString = @"發(fā)生位置錯誤";
alterTitle = @"提示";
break;
}
UIAlertView *locationFailAlert = [[UIAlertView alloc] initWithTitle:alterTitle message:errorString delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[locationFailAlert show];
}
注意事項:
1申窘、用戶隱私的保護
在iOS8中,定位服務(wù)發(fā)生了變化孔轴,需要用戶授權(quán)剃法。
在工程info.plist文件中添加下面值:
<key>NSLocationUsageDescription</key>
<string>需要您的同意,才能訪問位置</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>需要您的同意,才能在使用期間訪問位置</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>需要您的同意,才能始終訪問位置</string>
2、獲取應(yīng)用當(dāng)前的定位服務(wù)狀態(tài)
// 確定用戶的位置服務(wù)是否啟用
[CLLocationManager locationServicesEnabled]
// 位置服務(wù)是在設(shè)置中禁用
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
}