123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- //
- // BaiduMapManager.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2018/10/17.
- // Copyright © 2018 南鑫林. All rights reserved.
- //
- import UIKit
- public class BaiduMapManager: NSObject {
- private static let _sharedInstance = BaiduMapManager()
- private override init() {} // 私有化init方法
- class func shared() -> BaiduMapManager {
- return _sharedInstance
- }
- typealias LocationModelBlock = (_ locationModel: LocationModel) -> Void
- var locationModel = LocationModel.shared().getLocationModel()
- public func initBaiduMap() -> Void {
- BMKLocationAuth.sharedInstance()?.checkPermision(withKey: kBaiduMapAppKey, authDelegate: self)
- initLocation()
- }
- /// 单次定位
- func initLocation() {
- locationManager.requestLocation(withReGeocode: true, withNetworkState: true) { [weak self] location, state, error in
- if (error != nil) {
- LocationModel.shared().setLocationModel(locationModel: (self?.locationModel!)!)
- return
- } else {
- if let location = location {
- if let location = location.location {
- self?.locationModel?.latitude = String(describing: location.coordinate.latitude)
- self?.locationModel?.longitude = String(describing: location.coordinate.longitude)
- }
- if let rgcData = location.rgcData {
- self?.locationModel?.cityCode = rgcData.cityCode ?? ""
- self?.locationModel?.province = rgcData.province ?? ""
- self?.locationModel?.city = rgcData.city ?? ""
- self?.locationModel?.district = rgcData.district ?? ""
- self?.locationModel?.street = rgcData.street ?? ""
- self?.locationModel?.locationdescribe = rgcData.locationDescribe ?? ""
- }
- LocationModel.shared().setLocationModel(locationModel: (self?.locationModel!)!)
- }
- }
- self?.locationModel = LocationModel.shared().getLocationModel()
- }
- }
- /// 单次定位
- func initLocation(locationModelBlock:@escaping LocationModelBlock) -> Void {
- locationManager.requestLocation(withReGeocode: true, withNetworkState: true) { [weak self] location, state, error in
- if (error != nil) {
- LocationModel.shared().setLocationModel(locationModel:(self?.locationModel!)!)
- return
- } else {
- if let location = location {
- if let location = location.location {
- self?.locationModel?.latitude = String(describing: location.coordinate.latitude)
- self?.locationModel?.longitude = String(describing: location.coordinate.longitude)
- }
- if let rgcData = location.rgcData {
- self?.locationModel?.cityCode = rgcData.cityCode ?? ""
- self?.locationModel?.province = rgcData.province ?? ""
- self?.locationModel?.city = rgcData.city ?? ""
- self?.locationModel?.district = rgcData.district ?? ""
- self?.locationModel?.street = rgcData.street ?? ""
- self?.locationModel?.locationdescribe = rgcData.locationDescribe ?? ""
- }
- LocationModel.shared().setLocationModel(locationModel: (self?.locationModel!)!)
- }
- }
- self?.locationModel = LocationModel.shared().getLocationModel()
- locationModelBlock((self?.locationModel!)!)
- }
- }
- private lazy var locationManager: BMKLocationManager = {
- let locationManager = BMKLocationManager.init()
- //设置delegate
- locationManager.delegate = self
- //设置返回位置的坐标系类型
- locationManager.coordinateType = BMKLocationCoordinateType.BMK09LL
- //设置距离过滤参数
- locationManager.distanceFilter = kCLDistanceFilterNone
- //设置预期精度参数
- locationManager.desiredAccuracy = kCLLocationAccuracyBest
- //设置应用位置类型
- locationManager.activityType = CLActivityType.automotiveNavigation
- //设置是否自动停止位置更新
- locationManager.pausesLocationUpdatesAutomatically = false
- //设置是否允许后台定位
- locationManager.allowsBackgroundLocationUpdates = true
- //设置位置获取超时时间
- locationManager.locationTimeout = 10
- //设置获取地址信息超时时间
- locationManager.reGeocodeTimeout = 10
- return locationManager
- }()
- }
- // MARK: - 定位代理
- extension BaiduMapManager:BMKLocationManagerDelegate {
- /// 当定位发生错误时,会调用代理的此方法。
- public func bmkLocationManager(_ manager: BMKLocationManager, didFailWithError error: Error?) {
- NXLLog("定位错误");
- }
- /// 定位权限状态改变时回调函数
- public func bmkLocationManager(_ manager: BMKLocationManager, didChange status: CLAuthorizationStatus) {
- NXLLog("定位权限改变时候的回调");
- initLocation { (locationModel) in
- }
- }
- }
- // MARK: - 定位授权
- extension BaiduMapManager: BMKLocationAuthDelegate {
- public func onCheckPermissionState(_ iError: BMKLocationAuthErrorCode) {
- if (0 == iError.rawValue) {
- NXLLog("授权成功");
- initLocation { (locationModel) in
- }
- } else {
- NXLLog("授权失败 \(iError)");
- LocationModel.shared().setLocationModel(locationModel: locationModel!)
- }
- }
- }
|