YSFlutterView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.restricted {
  143. self.locationManager.requestWhenInUseAuthorization()
  144. self.locationManager.requestAlwaysAuthorization()
  145. }
  146. startSingleLocation();
  147. }
  148. }
  149. func mapView(_ mapView: QMapView!, viewFor overlay: QOverlay!) -> QOverlayView! {
  150. if overlay .isKind(of: QPolyline.self) {
  151. let polylineRender = QPolylineView.init(polyline: overlay as? QPolyline)!
  152. polylineRender.lineWidth = 11
  153. polylineRender.strokeColor = UIColor.orange
  154. return polylineRender
  155. }
  156. return nil
  157. }
  158. func startSingleLocation(){
  159. self.locationManager.requestLocation { (location, error) in
  160. let latitude = location?.location.coordinate.latitude ?? 40
  161. let longitude = location?.location.coordinate.longitude ?? 116
  162. if self.txView != nil{
  163. self.txView.setCenter(CLLocationCoordinate2DMake(latitude, longitude),animated: true)
  164. }
  165. self.methodChannel.invokeMethod("city", arguments: location?.city)
  166. self.methodChannel.invokeMethod("coordinate", arguments: "\(String(describing: latitude)),\(String(describing:longitude))")
  167. self.methodChannel.invokeMethod("address", arguments: ["lat":"\(latitude)","lng":"\(longitude)","title":location?.name as Any,"content":location?.address as Any,"airplaneName":location?.address as Any])
  168. }
  169. }
  170. func tencentLBSLocationManager(_ manager: TencentLBSLocationManager, didChange status: CLAuthorizationStatus) {
  171. startSingleLocation();
  172. }
  173. func mapView(_ mapView: QMapView!, viewFor annotation: QAnnotation!) -> QAnnotationView! {
  174. if annotation .isKind(of: QPointAnnotation.self) {
  175. let annotationIdentifier = "pointAnnotation"
  176. var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) ?? nil
  177. if pinView == nil {
  178. pinView = QPinAnnotationView.init(annotation: annotation, reuseIdentifier: annotationIdentifier)
  179. pinView?.canShowCallout = true
  180. pinView?.image = UIImage.init(named: "fj")
  181. }
  182. return pinView
  183. }
  184. return nil
  185. }
  186. func mapView(_ mapView: QMapView!, didTapAt coordinate: CLLocationCoordinate2D) {
  187. if self.points.count == 0 {
  188. print(coordinate)
  189. let revGeoOption = QMSReverseGeoCodeSearchOption.init()
  190. revGeoOption.setLocationWithCenter(coordinate)
  191. revGeoOption.poi_options = "address_format=short;radius=1000;page_size=20;page_index=1;policy=1;category=广场,大学,公交站,酒店"
  192. revGeoOption.get_poi = true
  193. self.search.search(with: revGeoOption)
  194. }
  195. }
  196. func mapView(_ mapView: QMapView!, didSelect view: QAnnotationView!) {
  197. let poi = view.annotation!
  198. if startLatLng != nil {
  199. if poi.coordinate.latitude != startLatLng.latitude || poi.coordinate.longitude != startLatLng.longitude {
  200. let title = poi.title?()
  201. let subtitle = poi.subtitle?()
  202. let id = self.idDict[title!]
  203. let map = ["lat":poi.coordinate.latitude,"lng":poi.coordinate.longitude,"title":title!,"content":subtitle!,"id":id!] as [String : Any]
  204. self.methodChannel.invokeMethod("endPoint", arguments: map)
  205. }
  206. }
  207. }
  208. func search(with reverseGeoCodeSearchOption: QMSReverseGeoCodeSearchOption, didReceive reverseGeoCodeSearchResult: QMSReverseGeoCodeSearchResult) {
  209. print(reverseGeoCodeSearchResult)
  210. let array = NSMutableArray()
  211. if reverseGeoCodeSearchResult.poi_count > 0 {
  212. reverseGeoCodeSearchResult.poisArray.forEach { (value) in
  213. let poi = value as! QMSReGeoCodePoi
  214. let dict = ["title":poi.title!,"content":poi.address!,"lat":"\(poi.location.latitude)","lng":"\(poi.location.longitude)"] as [String : Any]
  215. array.add(dict)
  216. }
  217. }else{
  218. let dict = ["title":"未知区域","content":"所选位置未获取到任何位置信息","lat":"\(reverseGeoCodeSearchResult.ad_info.location.latitude)","lng":"\(reverseGeoCodeSearchResult.ad_info.location.longitude)"] as [String : Any]
  219. array.add(dict)
  220. }
  221. self.methodChannel.invokeMethod("search", arguments: array)
  222. }
  223. func search(with searchOption: QMSSearchOption, didFailWithError error: Error) {
  224. print(error)
  225. }
  226. func view() -> UIView {
  227. return txView ?? UIView()
  228. }
  229. func tencentLBSLocationManager(_ manager: TencentLBSLocationManager, didFailWithError error: Error) {
  230. NSLog("error=========================================================>>\(error)")
  231. }
  232. deinit {
  233. self.methodChannel.setMethodCallHandler(nil)
  234. }
  235. }