高德定位慢 - iOS

因需求需要将高德定位集成进项目后,调用单次定位的方法,但发现获取定位方法回调数据很慢很慢 。。。

/**
 *  @brief 单次定位。如果当前正在连续定位,调用此方法将会失败,返回NO。\n该方法将会根据设定的 desiredAccuracy 去获取定位信息。如果获取的定位信息精确度低于 desiredAccuracy ,将会持续的等待定位信息,直到超时后通过completionBlock返回精度最高的定位信息。\n可以通过 stopUpdatingLocation 方法去取消正在进行的单次定位请求。
 *  @param withReGeocode 是否带有逆地理信息(获取逆地理信息需要联网)
 *  @param completionBlock 单次定位完成后的Block
 *  @return 是否成功添加单次定位Request
 */
- (BOOL)requestLocationWithReGeocode:(BOOL)withReGeocode completionBlock:(AMapLocatingCompletionBlock)completionBlock;

期间尝试过一些方法后,最终发现高德它是真的慢,但是也要解决问题呀,通过一通折腾后发现了一些路数;

 

解决办法

在初始化的时候有一个设置定位精准度的属性(即:setDesiredAccuracy),经过几经尝试,发现如下规律;

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;

设置为 Best 的时候定位大概耗时 8 秒;

设置为 NearestTenMeters 的时候定位大概耗时 5 秒;

设置为 HundredMeters 的时候定位大概耗时 2 秒;

设置为 ThreeKilometers 的时候定位大概秒定位到;

那么通过如上规律得知,精度约广耗时也就随之约短,那么也不能一味的为了减少耗时而过于降低定位的精准度;

最终依次将几个版本的精准度分别打包后安装在真机上尝试,发现前三项(即:Best、NearestTenMeters、HundredMeters)所获取到的定位数据信息一致,基本无偏差,故综合考量选择了耗时较短的 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];
}

/**
 获取定位数据

 @param sn      交互协议
 @param webView 控件
 */
- (void)getCurrentPositionBySnWith:(NSString *)sn AndWebView:(WKWebView *)webView  {
    [locationManagerGaoDe requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        NSLog(@"************ GaoDe 定位数据 ************");
        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 定位数据回调 ************\n%@", dict);

        // Callback
        [WKWebView appCallWebWithServiceResultToJsonSn:sn AndDataSourceObject:dict WithWKView:webView];
    }];
}

以上便是此次分享的内容,希望对大家有所帮助!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值