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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章