美文网首页iOS
高德定位慢 - iOS

高德定位慢 - iOS

作者: survivorsfyh | 来源:发表于2019-07-04 16:55 被阅读0次

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

/**
 *  @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];
    }];
}

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

相关文章

  • 高德定位慢 - iOS

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

  • 使用Category+runtime简单解决高德地图定位问题

    项目背景介绍项目需求 :使用定位功能,获取当前用户所在的地区打算使用的定位框架 : 高德定位平台 : iOS &&...

  • iOS源码博文集锦3

    iOS精选源码 高仿淘宝首页 登录动画 iOS高德二次封装,有定位,轨迹,语音实时导航,GPS纠偏..... 逗视...

  • iOS 定位 高德地图

    最近项目中有需求需要使用定位,上报经纬度和地址信息,还有可以在地图界面随意选择地点,因为和后台经纬度匹配的问题,所...

  • iOS 开发 高德-----定位

    单次定位 初始化 Block 回调(单次定位请求后Block回调) 持续定位 初始化 接收/处理位置更新 方向定位...

  • iOS 高德后台定位

    //后台定位self.locationManager.showsBackgroundLocationIndicat...

  • iOS 高德地图的使用

    iOS:高德地图的使用 本人花了点时间集成了高德地图的几乎所有的功能,包含:地图的显示、地图的绘制、地图的定位、地...

  • 高德地图,ios11定位不准问题

    iOS11刚出来的时候我刚好做了一个关于高德定位的功能,发现只有IOS11以上的系统会产生较大定位误差。由iOS1...

  • iOS 高德地图以及定位

    AppDelegate中 初始化高德地图 根据输入的地址获取经纬度,并让地图定位其位置

  • flutter Could not determine the

    接入高德定位(原^0.10.0+e74b91b),进行flutter upgrade之后,用iOS Run正常,安...

网友评论

    本文标题:高德定位慢 - iOS

    本文链接:https://www.haomeiwen.com/subject/suvrhctx.html