IOS 城市定位
前言:
獲取經緯度並且轉換成城市
IOS8定位失敗處理
獲取中文城市
1、樹立復雜的項目, 導入CoreLoation.framework:

2、在Info.plist中加上NSLocationAlwaysUsageDescription值為AlwaysLocation:

3、運用CLLocationManager對象停止定位:
_locationManger = [[CLLocationManager alloc] init]; _locationManger.delegate = self; [_locationManger requestAlwaysAuthorization];//IOS8需求加上,不然定位失敗 _locationManger.desiredAccuracy = kCLLocationAccuracyBest; //最准確形式 _locationManger.distanceFilter = 100.0f; //至多10米才懇求一次數據 [_locationManger startUpdatingLocation]; //開端定位
4、在CLLocationManagerDelegate代理辦法(iOS8)中獲取定位信息並且轉換成中文城市:
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"定位失敗");
[_locationManger stopUpdatingLocation];//封閉定位
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(@"定位成功");
[_locationManger stopUpdatingLocation];//封閉定位
CLLocation *newLocation = locations[0];
NSLog(@"%@",[NSString stringWithFormat:@"經度:%3.5f\n緯度:%3.5f",newLocation.coordinate.latitude, newLocation.coordinate.longitude]);
// 保管 設備 的以後言語
NSMutableArray *userDefaultLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
// 強迫 成 簡體中文
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-hans", nil nil] forKey:@"AppleLanguages"];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
NSDictionary *test = [placemark addressDictionary];
// Country(國度) State(城市) SubLocality(區)
NSLog(@"%@", [test objectForKey:@"State"]);
// 以後設備 在強迫將城市改成 簡體中文 後再復原之前的言語
[[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"];
}
}];
}
初次啟動時,會提示能否開啟定位並顯示NSLocationAlwaysUsageDescription的值:

其中字典test的詳細內容是:

感激閱讀,希望能協助到大家,謝謝大家對本站的支持!
【IOS 城市定位詳解及復雜實例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!