標籤:
註:在測定位功能的時候,比較多人會奇怪,為什麼代碼已經寫好了,可是測試的時候,只有初次開機模擬器定位代碼才有效。那是因為模擬器除了初次開機的時候會有預設的定位位置(預設位置是蘋果美國總部),其它時候都需要你手動的去開啟,在調試->位置->自定位置(填寫經緯度)。
.h
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)startGetLocation16 {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 *)location14 {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相應的屬性可以參考以下代碼:
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; //關聯的或利益相關的地標
如若你還需要通過城市名來擷取經緯度,則你可以使用以下方法:
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 }
IOS-CLLocationManager的定位功能