YSFlutterView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // YSFlutterView.swift
  3. // Runner
  4. //
  5. // Created by 大唐云 on 2021/6/3.
  6. //
  7. import Foundation
  8. import Flutter
  9. import TencentLBS
  10. class YSFlutterView: NSObject,FlutterPlatformView, TencentLBSLocationManagerDelegate, QMapViewDelegate, QMSSearchDelegate {
  11. var methodChannel:FlutterMethodChannel!
  12. var txView:QMapView!
  13. var locationManager:TencentLBSLocationManager!
  14. var search:QMSSearcher!
  15. var chooseList = NSMutableArray()
  16. var line:QPolyline!
  17. var markerDict = NSMutableDictionary()
  18. var lineDict = NSMutableDictionary()
  19. var points = NSMutableArray()
  20. var startLatLng:CLLocationCoordinate2D!
  21. var idDict = NSMutableDictionary()
  22. init(_ frame: CGRect,viewID: Int64,args :Any?,messenger :FlutterBinaryMessenger) {
  23. super.init()
  24. methodChannel = FlutterMethodChannel(name: "com.flutter.guide.MyFlutterView_\(viewID)", binaryMessenger: messenger)
  25. methodChannel.setMethodCallHandler { [self] (call, result) in
  26. if let dict = call.arguments as? Dictionary<String, Any>{
  27. if call.method=="setLocation"{
  28. let lat = dict["lat"] as! String
  29. let lng = dict["lng"] as! String
  30. let title = dict["title"] as! String
  31. let content = dict["content"] as! String
  32. let type = dict["type"] as! Int
  33. let latLng = CLLocationCoordinate2DMake(Double.init(lat)!, Double.init(lng)!)
  34. if line != nil {
  35. txView.removeOverlays([line])
  36. }
  37. chooseList.removeAllObjects()
  38. if type==0 {
  39. self.lineDict["startLine"] = latLng
  40. let point = QPointAnnotation.init()
  41. point.title = title;
  42. point.coordinate = latLng
  43. point.subtitle = content
  44. self.txView.addAnnotation(point)
  45. self.markerDict["startCity"] = point
  46. }else if type==1{
  47. self.lineDict["endLine"] = latLng
  48. let point = QPointAnnotation.init()
  49. point.title = title;
  50. point.coordinate = latLng
  51. point.subtitle = content
  52. self.txView.addAnnotation(point)
  53. self.markerDict["endCity"] = point
  54. }
  55. if self.lineDict["startLine"] != nil {
  56. chooseList.add(self.lineDict["startLine"]!)
  57. }
  58. if self.lineDict["endLine"] != nil {
  59. chooseList.add(self.lineDict["endLine"]!)
  60. }
  61. var coords = [CLLocationCoordinate2D]()
  62. self.chooseList.forEach { (value) in
  63. let valueLatlng = value as! CLLocationCoordinate2D
  64. coords.append(CLLocationCoordinate2DMake(valueLatlng.latitude, valueLatlng.longitude))
  65. }
  66. self.line = QPolyline.init(coordinates: &coords, count: UInt(self.chooseList.count))
  67. self.txView.addOverlays([self.line])
  68. self.txView.setCenter(latLng, animated: true)
  69. }else if call.method=="getLocation" {
  70. }else if call.method=="setPoints"{
  71. self.points.addObjects(from: dict["points"] as! [Any])
  72. let start = dict["startPoint"] as! NSDictionary
  73. let lat = start["lat"] as! String
  74. let lng = start["lng"] as! String
  75. self.startLatLng = CLLocationCoordinate2DMake(Double.init(lat)!, Double.init(lng)!)
  76. let range = start["range"] as! Double
  77. var level = 14
  78. if range>5000 {
  79. level = 1
  80. }else if range>2000 && range<=5000 {
  81. level = 2
  82. }else if range>1000 && range<=2000 {
  83. level = 3
  84. }else if range>500 && range<=1000 {
  85. level = 4
  86. }else if range>200 && range<=500 {
  87. level = 5
  88. }else if range>100 && range<=200 {
  89. level = 6
  90. }else if range>50 && range<=100 {
  91. level = 7
  92. }else if range>25 && range<=50 {
  93. level = 8
  94. }else if range>20 && range<=25 {
  95. level = 9
  96. }else if range>10 && range<=20 {
  97. level = 10
  98. }else if range>5 && range<=10 {
  99. level = 11
  100. }else if range>2 && range<=5 {
  101. level = 12
  102. }else if range>1 && range<=2 {
  103. level = 13
  104. }
  105. self.txView.setZoomLevel(CGFloat(level), animated: true)
  106. self.txView.setCenter(startLatLng, animated: true)
  107. self.txView.setRotation(45, animated: true)
  108. self.txView.setOverlooking(45, animated: true)
  109. points.forEach { (value) in
  110. let dict = value as! NSDictionary
  111. let lat = dict["lat"] as! String
  112. let lng = dict["lng"] as! String
  113. let title = dict["title"] as! String
  114. let content = dict["content"] as! String
  115. let id = dict["id"] as! String
  116. print("ios======================\(id))")
  117. self.idDict[title] = id
  118. let point = QPointAnnotation.init()
  119. point.coordinate = CLLocationCoordinate2DMake(Double.init(lat)!, Double.init(lng)!)
  120. point.title = title
  121. point.subtitle = content
  122. self.txView.addAnnotation(point)
  123. }
  124. }
  125. }
  126. }
  127. if(args is NSDictionary){
  128. let dict = args as! NSDictionary
  129. let type = dict["type"] as! Int
  130. if type==2 {
  131. txView = QMapView.init(frame: frame)
  132. txView.delegate = self
  133. txView.showsUserLocation = true
  134. search = QMSSearcher.init(delegate: self)
  135. }
  136. locationManager = TencentLBSLocationManager.init()
  137. self.locationManager.apiKey = "OOZBZ-NHIWS-OCAOO-6VB3W-ROSBH-ZYB7Z"
  138. self.locationManager.delegate = self
  139. self.locationManager.pausesLocationUpdatesAutomatically = false
  140. self.locationManager.requestLevel = TencentLBSRequestLevel.adminName
  141. let authorizationStatus = CLLocationManager.authorizationStatus()
  142. if authorizationStatus==CLAuthorizationStatus.notDetermined {
  143. self.locationManager.requestWhenInUseAuthorization()
  144. }
  145. self.locationManager.requestLocation { (location, error) in
  146. let latitude = location?.location.coordinate.latitude ?? 40
  147. let longitude = location?.location.coordinate.longitude ?? 116
  148. if self.txView != nil{
  149. self.txView.setCenter(CLLocationCoordinate2DMake(latitude, longitude),animated: true)
  150. }
  151. self.methodChannel.invokeMethod("city", arguments: location?.city)
  152. self.methodChannel.invokeMethod("coordinate", arguments: "\(String(describing: latitude)),\(String(describing:longitude))")
  153. self.methodChannel.invokeMethod("address", arguments: ["lat":"\(latitude)","lng":"\(longitude)","title":location?.name as Any,"content":location?.address as Any,"airplaneName":location?.address as Any])
  154. }
  155. }
  156. }
  157. func mapView(_ mapView: QMapView!, viewFor overlay: QOverlay!) -> QOverlayView! {
  158. if overlay .isKind(of: QPolyline.self) {
  159. let polylineRender = QPolylineView.init(polyline: overlay as? QPolyline)!
  160. polylineRender.lineWidth = 11
  161. polylineRender.strokeColor = UIColor.orange
  162. return polylineRender
  163. }
  164. return nil
  165. }
  166. func mapView(_ mapView: QMapView!, viewFor annotation: QAnnotation!) -> QAnnotationView! {
  167. if annotation .isKind(of: QPointAnnotation.self) {
  168. let annotationIdentifier = "pointAnnotation"
  169. var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) ?? nil
  170. if pinView == nil {
  171. pinView = QPinAnnotationView.init(annotation: annotation, reuseIdentifier: annotationIdentifier)
  172. pinView?.canShowCallout = true
  173. pinView?.image = UIImage.init(named: "fj")
  174. }
  175. return pinView
  176. }
  177. return nil
  178. }
  179. func mapView(_ mapView: QMapView!, didTapAt coordinate: CLLocationCoordinate2D) {
  180. if self.points.count == 0 {
  181. print(coordinate)
  182. let revGeoOption = QMSReverseGeoCodeSearchOption.init()
  183. revGeoOption.setLocationWithCenter(coordinate)
  184. revGeoOption.poi_options = "address_format=short;radius=1000;page_size=20;page_index=1;policy=1;category=广场,大学,公交站,酒店"
  185. revGeoOption.get_poi = true
  186. self.search.search(with: revGeoOption)
  187. }
  188. }
  189. func mapView(_ mapView: QMapView!, didSelect view: QAnnotationView!) {
  190. let poi = view.annotation!
  191. if startLatLng != nil {
  192. if poi.coordinate.latitude != startLatLng.latitude || poi.coordinate.longitude != startLatLng.longitude {
  193. let title = poi.title?()
  194. let subtitle = poi.subtitle?()
  195. let id = self.idDict[title!]
  196. let map = ["lat":poi.coordinate.latitude,"lng":poi.coordinate.longitude,"title":title!,"content":subtitle!,"id":id!] as [String : Any]
  197. self.methodChannel.invokeMethod("endPoint", arguments: map)
  198. }
  199. }
  200. }
  201. func search(with reverseGeoCodeSearchOption: QMSReverseGeoCodeSearchOption, didReceive reverseGeoCodeSearchResult: QMSReverseGeoCodeSearchResult) {
  202. print(reverseGeoCodeSearchResult)
  203. let array = NSMutableArray()
  204. if reverseGeoCodeSearchResult.poi_count > 0 {
  205. reverseGeoCodeSearchResult.poisArray.forEach { (value) in
  206. let poi = value as! QMSReGeoCodePoi
  207. let dict = ["title":poi.title!,"content":poi.address!,"lat":"\(poi.location.latitude)","lng":"\(poi.location.longitude)"] as [String : Any]
  208. array.add(dict)
  209. }
  210. }else{
  211. let dict = ["title":"未知区域","content":"所选位置未获取到任何位置信息","lat":"\(reverseGeoCodeSearchResult.ad_info.location.latitude)","lng":"\(reverseGeoCodeSearchResult.ad_info.location.longitude)"] as [String : Any]
  212. array.add(dict)
  213. }
  214. self.methodChannel.invokeMethod("search", arguments: array)
  215. }
  216. func search(with searchOption: QMSSearchOption, didFailWithError error: Error) {
  217. print(error)
  218. }
  219. func view() -> UIView {
  220. return txView ?? UIView()
  221. }
  222. func tencentLBSLocationManager(_ manager: TencentLBSLocationManager, didFailWithError error: Error) {
  223. NSLog("error=========================================================>>\(error)")
  224. }
  225. deinit {
  226. self.methodChannel.setMethodCallHandler(nil)
  227. }
  228. }