Просмотр исходного кода

ios jssdk 添加单次定位的api

fancy 5 лет назад
Родитель
Сommit
9cc940a91b

+ 5 - 0
o2ios/O2Platform/App/Work-工作/m/O2WebViewModels.swift

@@ -155,3 +155,8 @@ struct O2BizContactPickerResult: HandyJSON {
     var users: [O2PersonPickerItem]?
 }
 
+struct O2DeviceLocationResult: HandyJSON {
+    var latitude: Double?
+    var longitude: Double?
+    var address: String?
+}

+ 4 - 0
o2ios/O2Platform/Info.plist

@@ -135,6 +135,10 @@
 	<string>使用图片资料库</string>
 	<key>NSSpeechRecognitionUsageDescription</key>
 	<string>应用会识别您的语音来了解您的命令</string>
+	<key>UIBackgroundModes</key>
+	<array>
+		<string>location</string>
+	</array>
 	<key>UIMainStoryboardFile</key>
 	<string>login</string>
 	<key>UIRequiredDeviceCapabilities</key>

+ 85 - 0
o2ios/O2Platform/UI/BaseWebViewUIViewController.swift

@@ -26,6 +26,11 @@ open class BaseWebViewUIViewController: UIViewController {
     var delegate: BaseWebViewUIViewControllerJSDelegate?
     var o2WKScriptHandlers: [String : O2WKScriptMessageHandlerImplement] = [:]
     
+    //定位用
+    var locationCallBack: (String?) -> Void = {(result) in }
+    private var locService: BMKLocationManager?
+    private var searchAddress: BMKGeoCodeSearch?
+    
     
     //继承的子类如果要添加MessageHandler必须在theWebView方法前使用
     func addScriptMessageHandler(key: String, handler: O2WKScriptMessageHandlerImplement)  {
@@ -134,3 +139,83 @@ extension BaseWebViewUIViewController: WKScriptMessageHandler {
         }
     }
 }
+
+extension BaseWebViewUIViewController: BMKLocationManagerDelegate, BMKGeoCodeSearchDelegate {
+    public func startLocation() {
+        //开始定位
+        locService = BMKLocationManager()
+        if locService == nil {
+            locService?.desiredAccuracy = kCLLocationAccuracyBest
+            //设置返回位置的坐标系类型
+            locService?.coordinateType = .BMK09LL
+            //设置距离过滤参数
+            locService?.distanceFilter = kCLDistanceFilterNone;
+            //设置预期精度参数
+            locService?.desiredAccuracy = kCLLocationAccuracyBest;
+            //设置应用位置类型
+            locService?.activityType = .automotiveNavigation
+            //设置是否自动停止位置更新
+            locService?.pausesLocationUpdatesAutomatically = false
+            //定位返回geo地址信息
+            locService?.locatingWithReGeocode = true
+            //后台定位
+            locService?.allowsBackgroundLocationUpdates = true
+        }
+        if searchAddress == nil {
+            searchAddress = BMKGeoCodeSearch()
+        }
+        searchAddress?.delegate = self
+        locService?.delegate = self
+        locService?.startUpdatingLocation()
+    }
+    
+    public func stopLocation()  {
+        locService?.stopUpdatingLocation()
+        locService?.delegate = nil
+        searchAddress?.delegate = nil
+    }
+    
+    public func bmkLocationManager(_ manager: BMKLocationManager, didUpdate location: BMKLocation?, orError error: Error?) {
+        if let loc = location?.location {
+            DDLogDebug("当前位置,\(loc.coordinate.latitude),\(loc.coordinate.longitude)")
+            //根据经纬度搜索到地址
+            let re = BMKReverseGeoCodeSearchOption()
+            re.location = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
+            let _ = searchAddress?.reverseGeoCode(re)
+            
+        } else {
+            DDLogError("没有获取到定位信息!!!!!")
+        }
+    }
+    
+    public func bmkLocationManager(_ manager: BMKLocationManager, didFailWithError error: Error?) {
+        DDLogError("定位错误:\(String(describing: error?.localizedDescription))")
+    }
+    
+    public func onGetReverseGeoCodeResult(_ searcher: BMKGeoCodeSearch?, result: BMKReverseGeoCodeSearchResult?, errorCode error: BMKSearchErrorCode) {
+        //发送定位的实时位置及名称信息
+        if let location = result?.location {
+            var r = O2DeviceLocationResult()
+            r.latitude = location.latitude
+            r.longitude = location.longitude
+            r.address = result?.address ?? "没有获取到地址!"
+            locationCallBack(r.toJSONString())
+        }else {
+            var r = O2DeviceLocationResult()
+            r.address = "没有获取到地址!"
+            locationCallBack(r.toJSONString())
+            DDLogError("搜索地址失败, \(error)")
+        }
+        //结束定位
+        self.stopLocation()
+    }
+    
+    public func onGetGeoCodeResult(_ searcher: BMKGeoCodeSearch!, result: BMKGeoCodeSearchResult!, errorCode error: BMKSearchErrorCode) {
+        if Int(error.rawValue) == 0 {
+            DDLogDebug("result \(String(describing: result))")
+        } else {
+            DDLogDebug("result error  errorCode = \(Int(error.rawValue))")
+        }
+
+    }
+}

+ 25 - 1
o2ios/O2Platform/UI/o2JsApi/O2JsApiUtil.swift

@@ -52,6 +52,9 @@ class O2JsApiUtil: O2WKScriptMessageHandlerImplement {
                     case "device.scan":
                         scan(json: String(json))
                         break
+                    case "device.location":
+                        locationSingle(json: String(json))
+                        break
                     case "navigation.setTitle":
                         navigationSetTitle(json: String(json))
                         break
@@ -256,6 +259,27 @@ class O2JsApiUtil: O2WKScriptMessageHandlerImplement {
             DDLogError("getPhoneInfo, 解析json失败")
         }
     }
+    //单次定位
+    private func locationSingle(json: String) {
+        if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
+            if alert.callback != nil {
+                DispatchQueue.main.async {
+                    self.viewController.locationCallBack = { result in
+                        guard let r = result else {
+                            DDLogError("没有获取到位置信息")
+                            return
+                        }
+                        let callJs = "\(alert.callback!)('\(r)')"
+                        self.evaluateJs(callBackJs: callJs)
+                    }
+                    self.viewController.startLocation()
+                }
+            }
+        }else {
+            DDLogError("locationSingle, 解析json失败")
+        }
+    }
+    
     //设置t标题
     private func navigationSetTitle(json: String) {
         if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
@@ -321,4 +345,4 @@ class O2JsApiUtil: O2WKScriptMessageHandlerImplement {
             DDLogDebug("回调js执行完成!")
         })
     }
-}
+}