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

ios添加地图位置展现功能,js api支持

fancy 5 лет назад
Родитель
Сommit
327d887851

+ 2 - 6
o2ios/O2Platform/App/IM-聊天/IMChatViewController.swift

@@ -633,12 +633,8 @@ extension IMChatViewController: IMChatMessageDelegate {
     
     
     func openLocatinMap(info: IMMessageBodyInfo) {
-        let map = IMShowLocationViewController()
-        map.address = info.address
-        map.addressDetail = info.addressDetail
-        map.latitude = info.latitude
-        map.longitude = info.longitude
-        self.navigationController?.pushViewController(map, animated: false)
+        IMShowLocationViewController.pushShowLocation(vc: self, latitude: info.latitude, longitude: info.longitude,
+                                                      address: info.address, addressDetail: info.addressDetail)
     }
     
     func clickImageMessage(info: IMMessageBodyInfo) {

+ 109 - 0
o2ios/O2Platform/App/IM-聊天/IMShowLocationViewController.swift

@@ -7,8 +7,23 @@
 //
 
 import UIKit
+import MapKit
+import CocoaLumberjack
 
 class IMShowLocationViewController: UIViewController {
+    
+    
+    // MARK: - 在地图上展现一个位置,这里的经纬度是百度的经纬度
+    public static func pushShowLocation(vc: UIViewController, latitude: Double?, longitude: Double?,
+                                        address: String?, addressDetail: String?) {
+        let map = IMShowLocationViewController()
+        map.address = address
+        map.addressDetail = addressDetail
+        map.latitude = latitude
+        map.longitude = longitude
+        vc.navigationController?.pushViewController(map, animated: false)
+    }
+    
 
     private var mapView: BMKMapView!
     private var annotation: BMKPointAnnotation!
@@ -18,6 +33,9 @@ class IMShowLocationViewController: UIViewController {
     var latitude: Double?
     var longitude: Double?
     
+    //换算后的火星坐标 其他地图都用这个坐标
+    var gcj02Location: CLLocationCoordinate2D? = nil
+    
     override func viewDidLoad() {
         super.viewDidLoad()
         self.title = address
@@ -44,8 +62,99 @@ class IMShowLocationViewController: UIViewController {
         annotation.subtitle = addressDetail
         mapView.addAnnotation(annotation)
         
+        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "share"), style: .plain, target: self, action: #selector(openOtherMap))
+        
         
+        self.bd09Decrypt(CLLocationCoordinate2D(latitude: lat, longitude: lo)) { (ll) in
+            self.gcj02Location = ll
+        }
     }
     
+    
+    @objc private func openOtherMap() {
+        self.showSheetAction(title: nil, message: nil, actions: [
+            UIAlertAction(title: "百度地图", style: .default, handler: { (action) in
+                self.openMap(url: self.baiduMapUrl())
+            }),
+            UIAlertAction(title: "高德地图", style: .default, handler: { (action) in
+                self.openMap(url: self.aMapUrl())
+            }),
+            UIAlertAction(title: "腾讯地图", style: .default, handler: { (action) in
+                self.openMap(url: self.tencentMapUrl())
+            }),
+            UIAlertAction(title: "Apple 地图", style: .default, handler: { (action) in
+                self.openAppleMap()
+            })
+        ])
+    }
+    
+    private func openMap(url: URL?) {
+        if let u = url {
+            DDLogDebug("打开地图 url:\(u)")
+            UIApplication.shared.open(u, options: [:], completionHandler: nil)
+        }else {
+            DDLogError("没有生成url//。。。。。")
+            self.showError(title: "未安装该地图app,无法打开")
+        }
+    }
+    
+    private func openAppleMap() {
+        if let gcj02 = self.gcj02Location {
+            let currentLocation = MKMapItem.forCurrentLocation()
+            let toLocation = MKMapItem(placemark: MKPlacemark(coordinate: gcj02))
+            toLocation.name = self.address
+            MKMapItem.openMaps(with: [currentLocation, toLocation], launchOptions: [
+                MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
+                MKLaunchOptionsShowsTrafficKey: NSNumber(booleanLiteral: true)
+            ])
+        }else {
+            DDLogError("没有转化后的坐标。。。。。。。。")
+        }
+    }
  
+    //百度地图
+    private func baiduMapUrl() -> URL? {
+        if UIApplication.shared.canOpenURL(URL(string: "baidumap://")!) {
+            let url = "baidumap://map/direction?origin=我的位置&destination=latlng:\(latitude ?? 0),\(longitude ?? 0)|name:\(address ?? "")&mode=driving&coord_type=bd09ll&src=net.zoneland.m.o2oa"
+            return URL(string: url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) ?? "")
+        }
+         return nil
+    }
+    
+    //高德地图
+    private func aMapUrl() -> URL? {
+        //&sid=BGVIS1&did=BGVIS2
+        if UIApplication.shared.canOpenURL(URL(string: "iosamap://")!) {
+            let url = "iosamap://path?sourceApplication=O2OA&dlat=\(self.gcj02Location?.latitude ?? 0)&dlon=\(self.gcj02Location?.longitude ?? 0)&dname=\(self.address ?? "")&dev=0&t=0"
+            return URL(string: url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) ?? "")
+        }
+         return nil
+    }
+    
+    //腾讯地图
+    private func tencentMapUrl() -> URL? {
+        if UIApplication.shared.canOpenURL(URL(string: "qqmap://")!) {
+            let url = "qqmap://map/routeplan?type=drive&from=我的位置&to=\(self.address ?? "")&tocoord=\(self.gcj02Location?.latitude ?? 0),\(self.gcj02Location?.longitude ?? 0)&policy=1"
+            return URL(string: url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) ?? "")
+        }
+         return nil
+    }
+    
+    // MARK: - 百度BD09坐标转火星坐标
+    fileprivate let queue = DispatchQueue(label: "O2.LocationConverter.Converter")
+    fileprivate func bd09Decrypt(_ bd09Point:CLLocationCoordinate2D, result:@escaping (_ gcj02Point:CLLocationCoordinate2D) -> Void) {
+        self.queue.async {
+            let x = bd09Point.longitude - 0.0065
+            let y = bd09Point.latitude - 0.006
+            let z = sqrt(x * x + y * y) - 0.00002 * sin(y * .pi);
+            let theta = atan2(y, x) - 0.000003 * cos(x * .pi);
+            let resultPoint = CLLocationCoordinate2DMake(z * sin(theta), z * cos(theta))
+            DispatchQueue.main.async {
+                result(resultPoint)
+            }
+        }
+    }
+    
+    
+    
 }

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

@@ -93,6 +93,13 @@ class O2UtilNavigation: HandyJSON {
     var title: String!
     required init() {}
 }
+//地图展现位置
+struct O2UtilOpenMap: HandyJSON {
+    var address: String?
+    var addressDetail: String?
+    var latitude: Double?
+    var longitude: Double?
+}
 
 struct O2UtilPhoneInfo: HandyJSON {
     var screenWidth: String?

+ 22 - 0
o2ios/O2Platform/Assets.xcassets/im/share.imageset/Contents.json

@@ -0,0 +1,22 @@
+{
+  "images" : [
+    {
+      "idiom" : "universal",
+      "scale" : "1x"
+    },
+    {
+      "filename" : "share@2x.png",
+      "idiom" : "universal",
+      "scale" : "2x"
+    },
+    {
+      "filename" : "share@3x.png",
+      "idiom" : "universal",
+      "scale" : "3x"
+    }
+  ],
+  "info" : {
+    "author" : "xcode",
+    "version" : 1
+  }
+}

BIN
o2ios/O2Platform/Assets.xcassets/im/share.imageset/share@2x.png


BIN
o2ios/O2Platform/Assets.xcassets/im/share.imageset/share@3x.png


+ 6 - 0
o2ios/O2Platform/Info.plist

@@ -2,6 +2,12 @@
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
+	<key>LSApplicationQueriesSchemes</key>
+	<array>
+		<string>iosamap</string>
+		<string>qqmap</string>
+		<string>baidumap</string>
+	</array>
 	<key>CFBundleDevelopmentRegion</key>
 	<string>zh_CN</string>
 	<key>CFBundleDisplayName</key>

+ 19 - 0
o2ios/O2Platform/UI/o2JsApi/O2JsApiUtil.swift

@@ -55,6 +55,9 @@ class O2JsApiUtil: O2WKScriptMessageHandlerImplement {
                     case "device.location":
                         locationSingle(json: String(json))
                         break
+                    case "device.openMap":
+                        openMap(json: String(json))
+                        break
                     case "navigation.setTitle":
                         navigationSetTitle(json: String(json))
                         break
@@ -280,6 +283,22 @@ class O2JsApiUtil: O2WKScriptMessageHandlerImplement {
         }
     }
     
+    //打开地图位置
+    private func openMap(json: String) {
+        if let map = O2WebViewBaseMessage<O2UtilOpenMap>.deserialize(from: json) {
+            if let callback = map.callback, let data = map.data  {
+                DispatchQueue.main.async {
+                    IMShowLocationViewController.pushShowLocation(vc: self.viewController, latitude: data.latitude,
+                                                                  longitude: data.longitude, address: data.address, addressDetail: data.addressDetail)
+                    let callJs = "\(callback)('{}')"
+                    self.evaluateJs(callBackJs: callJs)
+                }
+            }
+        }else {
+            DDLogError("openMap, 解析json失败")
+        }
+    }
+    
     //设置t标题
     private func navigationSetTitle(json: String) {
         if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {