IOS 城市定位詳解及簡單實例
IOS 城市定位
前言:
獲取經(jīng)緯度并且轉(zhuǎn)換成城市
iOS8定位失敗解決
獲取中文城市
1、建立簡單的項目, 導(dǎo)入CoreLoation.framework:

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

3、使用CLLocationManager對象進(jìn)行定位:
_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];//關(guān)閉定位
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(@"定位成功");
[_locationManger stopUpdatingLocation];//關(guān)閉定位
CLLocation *newLocation = locations[0];
NSLog(@"%@",[NSString stringWithFormat:@"經(jīng)度:%3.5f\n緯度:%3.5f",newLocation.coordinate.latitude, newLocation.coordinate.longitude]);
// 保存 設(shè)備 的當(dāng)前語言
NSMutableArray *userDefaultLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
// 強(qiáng)制 成 簡體中文
[[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"]);
// 當(dāng)前設(shè)備 在強(qiáng)制將城市改成 簡體中文 后再還原之前的語言
[[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"];
}
}];
}
首次啟動時,會提示是否開啟定位并顯示NSLocationAlwaysUsageDescription的值:

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

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
IOS獲取當(dāng)前版本號 Bundle ID等信息的方法詳解
這篇文章主要介紹了IOS獲取當(dāng)前版本號 Bundle ID等信息的方法詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
iOS scrollview實現(xiàn)三屏復(fù)用循環(huán)廣告
這篇文章主要介紹了iOS scrollview實現(xiàn)三屏復(fù)用循環(huán)廣告,從服務(wù)器請求的廣告,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

