注:在測定位功能的時候,比較多人會奇怪,爲何代碼已經寫好了,但是測試的時候,只有首次啓動模擬器定位代碼纔有效。那是由於模擬器除了首次啓動的時候會有默認的定位位置(默認位置是蘋果美國總部),其它時候都須要你手動的去開啓,在調試->位置->自定位置(填寫經緯度)。git
.hpost
1 #import <CoreLocation/CoreLocation.h> 2 3 // 定位管理器,做用是:定位當前用戶的經度和緯度 4 @property (nonatomic, strong) CLLocationManager *locationManager; 5 @property (nonatomic, strong) CLGeocoder *geocoder;
.m測試
1 - (id)init 2 { 3 if (self = [super init]) { 4 // 定位管理器 5 _locationManager = [[CLLocationManager alloc] init]; 6 _geocoder = [[CLGeocoder alloc] init]; 7 // 當它定位完成,得到用戶的經度和緯度時,會通知代理 8 _locationManager.delegate = self; 9 } 10 11 return self; 12 } 13 14 #pragma mark 啓動定位 15 - (BOOL)startGetLocation 16 { 17 if ([CLLocationManager locationServicesEnabled]) {19 // 定位管理器 開始更新位置 20 [_locationManager startUpdatingLocation]; 21 return YES; 22 } else { 23 DLog(@"定位服務當前可能還沒有打開!");25 return NO; 26 } 27 }
CLGeocoder是一個地理編碼器,能夠把城市名反編碼成經緯度或相反,這個後面會有用到。而以上就是用CLLocationManager啓動定位的一個過程,先判斷程序的定位服務是否容許,而後用startUpdatingLocation啓動定位功能。編碼
1 #pragma mark - 定位管理器 代理方法 2 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 3 { 4 // 1.既然已經定位到了用戶當前的經緯度了,那麼可讓定位管理器 中止定位了 5 [_locationManager stopUpdatingLocation]; 6 // 2.而後,取出第一個位置,根據其經緯度,經過CLGeocoder反向解析,得到該位置所在的城市名稱 7 CLLocation *loc = [locations firstObject]; 8 9 [self getAddressByLocation:loc]; 10 } 11 12 #pragma mark 根據座標取得地名 13 -(void)getAddressByLocation:(CLLocation *)location 14 { 15 // __weak __typeof(self)weakSelf = self; 16 //反地理編碼 17 [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 18 CLPlacemark *placemark = [placemarks firstObject]; 19 NSString *locality = placemark.locality; 20 NSRange range = [locality rangeOfString:@"市"]; 21 if (range.length > 0) { 22 // 將最後一個字符【市】去掉 23 locality = [placemark.locality substringToIndex:placemark.locality.length - 1]; // 城市 24 } 25 NSLog(@"locality : %@", locality); 26 27 // if ([weakSelf.delegate respondsToSelector:@selector(didFinishLocations:error:)]) { 28 // weakSelf.locationState = SuSLocationFinish; 29 // [weakSelf.delegate didFinishLocations:[placemarks firstObject] error:error]; 30 // } 31 }]; 32 }
啓動後經過代理方法locationManager:didUpdateLocations:咱們就能夠得到定位位置的經緯度,而後就是須要用到CLGeocoder地理編碼器的時候了,用方法reverseGeocodeLocation:completionHandler:反編碼得到一個CLPlacemark對象,而後定位的信息就都存在該對象內了。CLPlacemark相應的屬性能夠參考如下代碼:atom
1 CLLocation *location = placemark.location;//位置 2 CLRegion *region=placemark.region;//區域 3 NSDictionary *addressDic= placemark.addressDictionary;//詳細地址信息字典,包含如下部分信息 4 NSString *name=placemark.name;//地名 5 NSString *thoroughfare=placemark.thoroughfare;//街道 6 NSString *subThoroughfare=placemark.subThoroughfare; //街道相關信息,例如門牌等 7 NSString *locality=placemark.locality; // 城市 8 NSString *subLocality=placemark.subLocality; // 城市相關信息,例如標誌性建築 9 NSString *administrativeArea=placemark.administrativeArea; // 州 10 NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其餘行政區域信息 11 NSString *postalCode=placemark.postalCode; //郵編 12 NSString *ISOcountryCode=placemark.ISOcountryCode; //國家編碼 13 NSString *country=placemark.country; //國家 14 NSString *inlandWater=placemark.inlandWater; //水源、湖泊 15 NSString *ocean=placemark.ocean; // 海洋 16 NSArray *areasOfInterest=placemark.areasOfInterest; //關聯的或利益相關的地標
如若你還須要經過城市名來獲取經緯度,則你可使用如下方法:spa
1 #pragma mark 根據地名肯定地理座標 2 - (void)getCoordinateByAddress:(NSString *)address 3 { 4 //地理編碼 5 [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { 6 //取得第一個地標,地標中存儲了詳細的地址信息,注意:一個地名可能搜索出多個地址 7 CLPlacemark *placemark = [placemarks firstObject]; 8 9 DLog(@"位置=%@-->緯度=%f----經度=%f", address, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude); 10 }]; 11 }