iOS 百度地图集成简谈

读懂人心.jpg
   目前我正在为一款关于车险的APP上线工作而忙碌着,里面有一个需要打开地图导航及定位功能的模块。所以今天我们来谈一谈有关于这方面集成的几个要点。。。
   第一步,很常规就不多说了,大家可以打开官网将这个SDK通过文件下载或者pod来获取相关SDK文件。
   第二步,集成定位功能的时候按照需求来下载是单次定位还是常定位的SDK

现在就给大家演示一下相关操作吧:
单次定位:

[[BMKLocationAuth sharedInstance] checkPermisionWithKey:@"RumL0R5SashrOBmzuI41BkL0FASiyiWk" authDelegate:self];
   //初始化实例
   _locationManager = [[BMKLocationManager alloc] init];
   //设置delegate
   _locationManager.delegate = self;
   //设置返回位置的坐标系类型
   _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
   //设置距离过滤参数
   _locationManager.distanceFilter = kCLDistanceFilterNone;
   //设置预期精度参数
   _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
   //设置应用位置类型
   _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
   //设置是否自动停止位置更新
   _locationManager.pausesLocationUpdatesAutomatically = NO;
   //设置是否允许后台定位
   //_locationManager.allowsBackgroundLocationUpdates = YES;
   //设置位置获取超时时间
   _locationManager.locationTimeout = 10;
   //设置获取地址信息超时时间
   _locationManager.reGeocodeTimeout = 10;
//这个方法是获取对应的location的值
[_locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
}];

常定位:

    _locationManager = [[BMKLocationManager alloc] init];
    //设置delegate
    _locationManager.delegate = self;
    //设置返回位置的坐标系类型
    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    //设置距离过滤参数
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //设置预期精度参数
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置应用位置类型
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //设置是否自动停止位置更新
    _locationManager.pausesLocationUpdatesAutomatically = YES;
    //设置是否允许后台定位
    _locationManager.allowsBackgroundLocationUpdates = NO;
    //设置位置获取超时时间
    _locationManager.locationTimeout = 10;
    //设置获取地址信息超时时间
    _locationManager.reGeocodeTimeout = 10;

然后常定位需要实现对应的代理方法:

  • (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error {
    };
    如果需求上面还需要获取对应的具体地址,比如说城市等。这时候还得要一个反编译的操作:
[geocoder reverseGeocodeLocation:location.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (! error) {
            if ([placemarks count] > 0) {
                CLPlacemark *placemark = [placemarks firstObject];

                // 获取城市
                NSString *city = placemark.locality;
                if (!city) {
                    // 6
                    city = placemark.administrativeArea;
                }
                self.city = city;

//                [self requestForMyMapAddress];
            } else if ([placemarks count] == 0) {
                //                [TSMessage showNotificationWithTitle:@"GPS故障"
                //                                            subtitle:@"定位城市失败"
                //                                                type:TSMessageNotificationTypeError];
            }
        } else {
            //            [TSMessage showNotificationWithTitle:@"网络错误"
            //                                        subtitle:@"请检查您的网络"
            //                                            type:TSMessageNotificationTypeError];
        }
    }];
  第三步,将所有的权限问题检查一遍,没有写的添加到plist文件上面。(当然常定位对应的权限要多几个,注意添加)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容