第一步: 在Info.plist中添加以下兩個鍵组力,iOS8新增的定位提示語,值可以為空,但必須添加莱褒,否則定位沒有回調
NSLocationWhenInUseUsageDescription
-
NSLocationAlwaysUsageDescription
<p>
第二步: 導入頭文件
#import <CoreLocation/CoreLocation.h>
<p>
第三步:添加代理
<CLLocationManagerDelegate>
<p>
第四步:聲明定位管理者對象
CLLocationManager* locationManager;// 定位管理者
<p>
第五步:實現定位方法
<p>
//判斷是否是模擬器的宏
#define isSimulator ([[UIDevice currentDevice].model isEqualToString:@"iPhone Simulator"] || [[UIDevice currentDevice].model isEqualToString:@"iPad Simulator"])
/*
* 在模擬器中是無法開啟定位的,為了實現在模擬器中調用定位服務,重寫startUpdatingLocation方法模擬定位
* 在真機中屏蔽該方法
*/
- (void)startUpdatingLocationInSimulator {
#if TARGET_IPHONE_SIMULATOR
float latitude = 32.061;
float longitude = 118.79125;
CLLocation *setLocation= [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[[self delegate]locationManager:self didUpdateToLocation:setLocation fromLocation:nil];
#endif
}
// 開始定位
-(void)startLocation {
locationManager = [[CLLocationManager alloc] init];
if([CLLocationManager locationServicesEnabled]){
// 設置代理
locationManager.delegate = self;
// 設置更新地理信息的最短距離,單位米
locationManager.distanceFilter = 10.0f;
// 設置GPS精確度
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// iOS8新增方法荒叼,必須在startUpdatingLocation前調用轿偎,檢查NSLocationWhenInUseUsageDescription是否添加進Info.plist
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
if (isSimulator) {// 如果是在模擬器,調用模擬地點
[locationManager startUpdatingLocationInSimulator];
}
else {
[locationManager startUpdatingLocation];
}
}
else {// 未開啟定位
[self hideActivityIndicator];
}
}
// 停止定位
- (void)stopLocation {
[locationManager stopUpdatingLocation];
locationManager = nil;
}
GPS精度從高到低(越精確越耗電)
- kCLLocationAccuracyBestForNavigation // (最精確)
- kCLLocationAccuracyBest
- kCLLocationAccuracyNearestTenMeters
- kCLLocationAccuracyHundredMeters
- kCLLocationAccuracyKilometer
- kCLLocationAccuracyThreeKilometers
第六步:實現代理方法
// 定位代理經緯度回調
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self hideActivityIndicator];// 停止菊花
[self stopLocation];
[self getlocationFromCLLocation:newLocation];
}
// iOS6新增回調方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[self hideActivityIndicator];// 停止菊花
[self stopLocation];
[self getlocationFromCLLocation:[locations lastObject]];
}
// 地理信息反編碼
- (void)getlocationFromCLLocation:(CLLocation *)location {
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error){
for (CLPlacemark * placemark in placemarks) {
// 地理信息的Dictionary
// Country(國家被廓,例:中國)
// State (省坏晦,例:廣東省)
// City (市,例:廣州市)
// SubLocality(區(qū)嫁乘,例:海珠區(qū))
// Street (街道昆婿,例:珠江東路)
// Thoroughfare(大道,例:珠江東路)
// Name (地址蜓斧,例:中國廣東省廣州市天河區(qū)冼村街道珠江東路)
// FormattedAddressLines(地址編碼仓蛆,轉碼后的地址)
// CountryCode(國家編碼,例:CN)
NSDictionary *test = [placemark addressDictionary];
// TODO
}
}];
}
更多信息