高德定位慢 - 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];
    }];
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 也许不知多少年过去了 我的生命依旧如春雨后的生命般蓬勃 就像一只大雁偶然飞过山谷 漫山的桃花在春风中摇晃、抖动 这...
    钟春生的诗阅读 921评论 0 1
  • “你别叫了,才几点!” “怎么了主人?” “我还要睡会儿…你好烦。” “呜呜,你婆娘半夜叫得狼嚎似的,你都没说啥,...
    简不阅读 1,558评论 0 1
  • 1.获取表单的所有数据 2. 获取表单的部分字段数据 ES6中Array.find()和findIndex()函数...
    风之伤_3eed阅读 2,166评论 0 0
  • 那些陪着我长大的美.慢慢补充. 1.狼牙月,伊人憔悴,我举杯饮尽了风雪.是谁打翻前世柜,惹尘埃是非. 缘字诀,几番...
    心月兔阅读 2,707评论 0 1

友情链接更多精彩内容