iOS 反编码地址获取是否在国内以及手机模拟定位

1.用到的方法
CLGeocoder 的一个方法

- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

2.调用实例

if ([self.geocoder isGeocoding]) {
        [self.geocoder cancelGeocode];
    }
    [self.geocoder reverseGeocodeLocation:location
                        completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                            if (error || placemarks.count == 0) {
                                LyLog(@"反编码地址错误:%@[placemarks:%@]",error,placemarks);                    
                            }else{
                                CLPlacemark * placemark = placemarks[0];
                                LyLog(@"反编码地址成功:%@",placemark.country);
                                if (([placemark.ISOcountryCode isEqualToString:@"CN"] || [placemark.ISOcountryCode isEqualToString:@"HK"] || [placemark.ISOcountryCode isEqualToString:@"MO"] || [placemark.ISOcountryCode isEqualToString:@"TW"])) {

                                }else{

                                }
                            }
                        }];

3.注意的坑

说明文档中有提到:
使用Geocoding是有频率限制的,如果短期内调用次数过多会返回错误(当超过最大次数时,geocoder 会返回kCLErrorNetwork 错误)。下面是几个注意的点:
1)用户的操作最多发送一个Geocoding请求
2)如果用户需要反编码同一个地址,重复利用反编码结果,当用户移动了一段距离并且过了一定时间后再发送Geocoding请求。每分钟不应该超过一个Geocoding请求。
3)当用户不是马上看到反编码结果时,不要发送反编码请求。比如应用在inactive或者background状态下,不要发请求。
要获取详细的反编码信息,设备需要连接网络。尽管geocoder会在本地存储包括本地化国家名称和ISO country code 的位置信息。如果对于一个特殊的位置这些信息不可用,geocoder会在completion block中报错。

4.相关内容
模拟定位
在Xcode中设置 Edit Scheme -> run -> options 下 corelocation Allow Location Simulation 前面打钩,
Default Location 点击会有下拉菜单,可选择模拟的位置。【自己测试,选择除了中国之外的位置,反编码地址不成功,没有找到原因】
另外想模拟除了Default Location 列表中的其他位置,可参照以下做法
http://www.cnblogs.com/cocoajin/p/6108600.html
在工程新建gpx类型的文件,命名为location.gpx。文件内容如下

typedef enum CLError : NSInteger {
    kCLErrorLocationUnknown = 0,
    kCLErrorDenied,
    kCLErrorNetwork,
    kCLErrorHeadingFailure,
    kCLErrorRegionMonitoringDenied,
    kCLErrorRegionMonitoringFailure,
    kCLErrorRegionMonitoringSetupDelayed,
    kCLErrorRegionMonitoringResponseDelayed,
    kCLErrorGeocodeFoundNoResult,
    kCLErrorGeocodeFoundPartialResult,
    kCLErrorGeocodeCanceled,
    kCLErrorDeferredFailed,
    kCLErrorDeferredNotUpdatingLocation,
    kCLErrorDeferredAccuracyTooLow,
    kCLErrorDeferredDistanceFiltered,
    kCLErrorDeferredCanceled,
    kCLErrorRangingUnavailable,
    kCLErrorRangingFailure
} CLError;
kCLErrorLocationUnknown
The location manager was unable to obtain a location value right now.

kCLErrorDenied
Access to the location service was denied by the user.

kCLErrorNetwork
The network was unavailable or a network error occurred.

kCLErrorHeadingFailure
The heading could not be determined.

kCLErrorRegionMonitoringDenied
Access to the region monitoring service was denied by the user. 

kCLErrorRegionMonitoringFailure
A registered region cannot be monitored. Monitoring can fail if the app has exceeded the maximum number of regions that it can monitor simultaneously. Monitoring can also fail if the region’s radius distance is too large.

kCLErrorRegionMonitoringSetupDelayed
Core Location could not initialize the region monitoring feature immediately.

kCLErrorRegionMonitoringResponseDelayed
Core Location will deliver events but they may be delayed. Possible keys in the user information dictionary are described in Error User Info Keys.

kCLErrorGeocodeFoundNoResult
The geocode request yielded no result. 

kCLErrorGeocodeFoundPartialResult
The geocode request yielded a partial result. 

kCLErrorGeocodeCanceled
The geocode request was canceled. 

kCLErrorDeferredFailed
The location manager did not enter deferred mode for an unknown reason. This error can occur if GPS is unavailable, not active, or is temporarily interrupted. If you get this error on a device that has GPS hardware, the solution is to try again.

kCLErrorDeferredNotUpdatingLocation
The location manager did not enter deferred mode because location updates were already disabled or paused. 

kCLErrorDeferredAccuracyTooLow
Deferred mode is not supported for the requested accuracy. The accuracy must be set to kCLLocationAccuracyBest or kCLLocationAccuracyBestForNavigation. 

kCLErrorDeferredDistanceFiltered
Deferred mode does not support distance filters. Set the distance filter to kCLDistanceFilterNone. 

kCLErrorDeferredCanceled
The request for deferred updates was canceled by your app or by the location manager. This error is returned if you call the disallowDeferredLocationUpdates method or schedule a new deferred update before the previous deferred update request is processed. The location manager may also report this error too. For example, if the app is in the foreground when a new location is determined, the location manager cancels deferred updates and delivers the location data to your app.

kCLErrorRangingUnavailable
Ranging is disabled. This might happen if the device is in Airplane mode or if Bluetooth or location services are disabled.

kCLErrorRangingFailure
A general ranging error occurred.
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页