iOS地图定位系统语言为英文时,返回中文位置信息

需要在定位前后对系统语言进行转换:

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //获取用户位置
    CLLocation *location = [locations lastObject];
    //停止定位
    [_locationManager stopUpdatingLocation];
    
    //当定位成功后,如果horizontalAccuracy大于0,说明定位有效
    if (location.horizontalAccuracy < 0) {
        return;
    }
    //系统语言为英文时返回中文编码
    NSMutableArray *defaultLanguages = [LRH_USERDEFAULT objectForKey:@"AppleLanguages"];
    [LRH_USERDEFAULT setObject:[NSArray arrayWithObjects:@"zh-hans",nil] forKey:@"AppleLanguages"];
    
    //逆地理编码
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    __weak typeof(self) weakSelf = self;
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        NSString *cityName = @"";
        if (error) {
            LRHLog(@"没有找到此经纬度的地理信息");
        } else {
            CLPlacemark *pmark = [placemarks firstObject];
            cityName = pmark.locality;
        }
        
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getLocationCityWith:withCityName:)]) {
            [weakSelf.delegate getLocationCityWith:self withCityName:cityName];
        }
        //还原系统语言
        [LRH_USERDEFAULT setObject:defaultLanguages forKey:@"AppleLanguages"];
    }];
}

 自己写的一个定位管理类:

//
//  LRHLocationUtils.h
//  LRHAPP
//
//  Created by ZY on 2017/4/6.
//  Copyright © 2017年 Vision Credit Ltd. All rights reserved.
//

#import <Foundation/Foundation.h>

@class LRHLocationUtils;
@protocol LRHLocationUtilsDelegate <NSObject>

- (void)getLocationCityWith:(LRHLocationUtils *)locationUtils withCityName:(NSString *)cityName;

@end

@interface LRHLocationUtils : NSObject

+ (instancetype)shareManager;

@property (nonatomic,weak) id <LRHLocationUtilsDelegate>delegate;
@property (nonatomic,copy) NSString *userCitys;
//开始定位
- (void)startGetLocation;

@end
View Code
//
//  LRHLocationUtils.m
//  LRHAPP
//
//  Created by ZY on 2017/4/6.
//  Copyright © 2017年 Vision Credit Ltd. All rights reserved.
//

#import "LRHLocationUtils.h"
#import <CoreLocation/CoreLocation.h>

@interface LRHLocationUtils ()<CLLocationManagerDelegate>

@property(nonatomic,strong)CLLocationManager *locationManager;

@end

@implementation LRHLocationUtils

- (void)dealloc{
    _locationManager.delegate = nil;
}

+ (instancetype)shareManager{
    static LRHLocationUtils *locationUtls = nil;
    static dispatch_once_t oncetoken;
    dispatch_once(&oncetoken, ^{
        locationUtls = [[LRHLocationUtils alloc]init];
    });
    return locationUtls;
}


- (instancetype)init{
    self = [super init];
    if (self) {
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        //设置定位精准度
        _locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
        
        if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            //当用户使用的时候授权
            [_locationManager requestWhenInUseAuthorization];
        }
    }
    return self;
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //获取用户位置
    CLLocation *location = [locations lastObject];
    //停止定位
    [_locationManager stopUpdatingLocation];
    
    //当定位成功后,如果horizontalAccuracy大于0,说明定位有效
    if (location.horizontalAccuracy < 0) {
        return;
    }
    //系统语言为英文时返回中文编码
    NSMutableArray *defaultLanguages = [LRH_USERDEFAULT objectForKey:@"AppleLanguages"];
    [LRH_USERDEFAULT setObject:[NSArray arrayWithObjects:@"zh-hans",nil] forKey:@"AppleLanguages"];
    
    //逆地理编码
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    __weak typeof(self) weakSelf = self;
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        NSString *cityName = @"";
        if (error) {
            LRHLog(@"没有找到此经纬度的地理信息");
        } else {
            CLPlacemark *pmark = [placemarks firstObject];
            cityName = pmark.locality;
        }
        
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getLocationCityWith:withCityName:)]) {
            [weakSelf.delegate getLocationCityWith:self withCityName:cityName];
        }
        //还原系统语言
        [LRH_USERDEFAULT setObject:defaultLanguages forKey:@"AppleLanguages"];
    }];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    if (_delegate && [_delegate respondsToSelector:@selector(getLocationCityWith:withCityName:)]) {
        [_delegate getLocationCityWith:self withCityName:@""];
    }
}

#pragma mark - private method

- (void)startGetLocation{
    [_locationManager startUpdatingLocation];
}

@end
View Code

 

posted @ 2017-07-04 09:59  Crazy_ZY  阅读(3264)  评论(0编辑  收藏  举报