因需求需要將高德定位集成進(jìn)項(xiàng)目后问慎,調(diào)用單次定位的方法,但發(fā)現(xiàn)獲取定位方法回調(diào)數(shù)據(jù)很慢很慢 挤茄。如叼。。
/**
* @brief 單次定位穷劈。如果當(dāng)前正在連續(xù)定位笼恰,調(diào)用此方法將會失敗踊沸,返回NO。\n該方法將會根據(jù)設(shè)定的 desiredAccuracy 去獲取定位信息社证。如果獲取的定位信息精確度低于 desiredAccuracy 逼龟,將會持續(xù)的等待定位信息,直到超時(shí)后通過completionBlock返回精度最高的定位信息追葡。\n可以通過 stopUpdatingLocation 方法去取消正在進(jìn)行的單次定位請求腺律。
* @param withReGeocode 是否帶有逆地理信息(獲取逆地理信息需要聯(lián)網(wǎng))
* @param completionBlock 單次定位完成后的Block
* @return 是否成功添加單次定位Request
*/
- (BOOL)requestLocationWithReGeocode:(BOOL)withReGeocode completionBlock:(AMapLocatingCompletionBlock)completionBlock;
期間嘗試過一些方法后,最終發(fā)現(xiàn)高德它是真的慢宜肉,但是也要解決問題呀匀钧,通過一通折騰后發(fā)現(xiàn)了一些路數(shù);
解決辦法
在初始化的時(shí)候有一個(gè)設(shè)置定位精準(zhǔn)度的屬性(即:setDesiredAccuracy)谬返,經(jīng)過幾經(jīng)嘗試之斯,發(fā)現(xiàn)如下規(guī)律;
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation API_AVAILABLE(ios(4.0), macos(10.7));
extern const CLLocationAccuracy kCLLocationAccuracyBest;
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
設(shè)置為 Best 的時(shí)候定位大概耗時(shí) 8 秒遣铝;
設(shè)置為 NearestTenMeters 的時(shí)候定位大概耗時(shí) 5 秒吊圾;
設(shè)置為 HundredMeters 的時(shí)候定位大概耗時(shí) 2 秒;
設(shè)置為 ThreeKilometers 的時(shí)候定位大概秒定位到翰蠢;
那么通過如上規(guī)律得知项乒,精度約廣耗時(shí)也就隨之約短,那么也不能一味的為了減少耗時(shí)而過于降低定位的精準(zhǔn)度梁沧;
最終依次將幾個(gè)版本的精準(zhǔn)度分別打包后安裝在真機(jī)上嘗試檀何,發(fā)現(xiàn)前三項(xiàng)(即:Best、NearestTenMeters廷支、HundredMeters)所獲取到的定位數(shù)據(jù)信息一致频鉴,基本無偏差,故綜合考量選擇了耗時(shí)較短的 HundredMeters恋拍;
/**
高德定位初始化
*/
- (void)settingGaoDeConfig {
[AMapServices sharedServices].apiKey = APPKEY_GaoDe;
locationManagerGaoDe = [[AMapLocationManager alloc] init];
// [locationManagerGaoDe setDesiredAccuracy:kCLLocationAccuracyBest];// 8 5 2
// [locationManagerGaoDe setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManagerGaoDe setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[locationManagerGaoDe setPausesLocationUpdatesAutomatically:NO];
[locationManagerGaoDe setAllowsBackgroundLocationUpdates:YES];
[locationManagerGaoDe setReGeocodeTimeout:5.f];
[locationManagerGaoDe setLocationTimeout:5.f];
[locationManagerGaoDe setDelegate:self];
}
/**
獲取定位數(shù)據(jù)
@param sn 交互協(xié)議
@param webView 控件
*/
- (void)getCurrentPositionBySnWith:(NSString *)sn AndWebView:(WKWebView *)webView {
[locationManagerGaoDe requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
NSLog(@"************ GaoDe 定位數(shù)據(jù) ************");
NSLog(@"%@", location);
NSLog(@"緯度 %f", location.coordinate.latitude);
NSLog(@"精度 %f", location.coordinate.longitude);
NSLog(@"%@", regeocode);
NSLog(@"%@", error);
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSUInteger code = 0;
if (error) {
code = -1;
[dict setObject:[NSNumber numberWithInteger:-1] forKey:@"code"];
} else {
[dict setValue:regeocode.formattedAddress forKey:@"address"];
[dict setValue:regeocode.country forKey:@"country"];
[dict setValue:regeocode.adcode forKey:@"adCode"];
[dict setObject:[NSNumber numberWithFloat:location.coordinate.longitude] forKey:@"longitude"];
[dict setValue:regeocode.city forKey:@"city"];
[dict setValue:regeocode.district forKey:@"district"];
[dict setObject:[NSNumber numberWithFloat:location.coordinate.latitude] forKey:@"latitude"];
[dict setValue:regeocode.province forKey:@"province"];
[dict setValue:regeocode.citycode forKey:@"cityCode"];
[dict setObject:[NSNumber numberWithInteger:code] forKey:@"code"];
}
NSLog(@"************ GaoDe 定位數(shù)據(jù)回調(diào) ************\n%@", dict);
// Callback
[WKWebView appCallWebWithServiceResultToJsonSn:sn AndDataSourceObject:dict WithWKView:webView];
}];
}
以上便是此次分享的內(nèi)容垛孔,希望對大家有所幫助!