IOS 城市定位詳解及簡單實例
IOS 城市定位
前言:
獲取經(jīng)緯度并且轉(zhuǎn)換成城市
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米才請求一次數(shù)據(jù) [_locationManger startUpdatingLocation]; //開始定位
4、在CLLocationManagerDelegate代理方法(iOS8)中獲取定位信息并且轉(zhuǎn)換成中文城市:
- (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:@"經(jīng)度:%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(區(qū))
NSLog(@"%@", [test objectForKey:@"State"]);
// 當前設備 在強制將城市改成 簡體中文 后再還原之前的語言
[[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"];
}
}];
}
首次啟動時,會提示是否開啟定位并顯示NSLocationAlwaysUsageDescription的值:

其中字典test的具體內(nèi)容是:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
iOS開發(fā)實戰(zhàn)之Label全方位對齊的輕松實現(xiàn)
這篇文章主要給大家介紹了關于iOS開發(fā)實戰(zhàn)之輕松實現(xiàn)Label全方位對齊的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-10-10
IOS開發(fā)中禁止NavigationController的向右滑動返回
這篇文章主要介紹了IOS開發(fā)中禁止NavigationController的向右滑動返回的相關資料,需要的朋友可以參考下2017-03-03

