123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- //
- // YSFlutterView.swift
- // Runner
- //
- // Created by 大唐云 on 2021/6/3.
- //
- import Foundation
- import Flutter
- import TencentLBS
- class YSFlutterView: NSObject,FlutterPlatformView, TencentLBSLocationManagerDelegate, QMapViewDelegate, QMSSearchDelegate {
-
- var methodChannel:FlutterMethodChannel!
- var txView:QMapView!
- var locationManager:TencentLBSLocationManager!
- var search:QMSSearcher!
- var chooseList = NSMutableArray()
- var line:QPolyline!
- var markerDict = NSMutableDictionary()
- var lineDict = NSMutableDictionary()
- var points = NSMutableArray()
- var startLatLng:CLLocationCoordinate2D!
- var idDict = NSMutableDictionary()
-
- init(_ frame: CGRect,viewID: Int64,args :Any?,messenger :FlutterBinaryMessenger) {
- super.init()
- methodChannel = FlutterMethodChannel(name: "com.flutter.guide.MyFlutterView_\(viewID)", binaryMessenger: messenger)
- methodChannel.setMethodCallHandler { [self] (call, result) in
- if let dict = call.arguments as? Dictionary<String, Any>{
- if call.method=="setLocation"{
- let lat = dict["lat"] as! String
- let lng = dict["lng"] as! String
- let title = dict["title"] as! String
- let content = dict["content"] as! String
- let type = dict["type"] as! Int
- let latLng = CLLocationCoordinate2DMake(Double.init(lat)!, Double.init(lng)!)
- if line != nil {
- txView.removeOverlays([line])
- }
- chooseList.removeAllObjects()
- if type==0 {
- self.lineDict["startLine"] = latLng
- let point = QPointAnnotation.init()
- point.title = title;
- point.coordinate = latLng
- point.subtitle = content
- self.txView.addAnnotation(point)
- self.markerDict["startCity"] = point
- }else if type==1{
- self.lineDict["endLine"] = latLng
- let point = QPointAnnotation.init()
- point.title = title;
- point.coordinate = latLng
- point.subtitle = content
- self.txView.addAnnotation(point)
- self.markerDict["endCity"] = point
- }
- if self.lineDict["startLine"] != nil {
- chooseList.add(self.lineDict["startLine"]!)
- }
- if self.lineDict["endLine"] != nil {
- chooseList.add(self.lineDict["endLine"]!)
- }
- var coords = [CLLocationCoordinate2D]()
- self.chooseList.forEach { (value) in
- let valueLatlng = value as! CLLocationCoordinate2D
- coords.append(CLLocationCoordinate2DMake(valueLatlng.latitude, valueLatlng.longitude))
- }
- self.line = QPolyline.init(coordinates: &coords, count: UInt(self.chooseList.count))
- self.txView.addOverlays([self.line])
- self.txView.setCenter(latLng, animated: true)
-
- }else if call.method=="getLocation" {
-
- }else if call.method=="setPoints"{
- self.points.addObjects(from: dict["points"] as! [Any])
- let start = dict["startPoint"] as! NSDictionary
- let lat = start["lat"] as! String
- let lng = start["lng"] as! String
- self.startLatLng = CLLocationCoordinate2DMake(Double.init(lat)!, Double.init(lng)!)
- let range = start["range"] as! Double
- var level = 14
- if range>5000 {
- level = 1
- }else if range>2000 && range<=5000 {
- level = 2
- }else if range>1000 && range<=2000 {
- level = 3
- }else if range>500 && range<=1000 {
- level = 4
- }else if range>200 && range<=500 {
- level = 5
- }else if range>100 && range<=200 {
- level = 6
- }else if range>50 && range<=100 {
- level = 7
- }else if range>25 && range<=50 {
- level = 8
- }else if range>20 && range<=25 {
- level = 9
- }else if range>10 && range<=20 {
- level = 10
- }else if range>5 && range<=10 {
- level = 11
- }else if range>2 && range<=5 {
- level = 12
- }else if range>1 && range<=2 {
- level = 13
- }
- self.txView.setZoomLevel(CGFloat(level), animated: true)
- self.txView.setCenter(startLatLng, animated: true)
- self.txView.setRotation(45, animated: true)
- self.txView.setOverlooking(45, animated: true)
- points.forEach { (value) in
- let dict = value as! NSDictionary
- let lat = dict["lat"] as! String
- let lng = dict["lng"] as! String
- let title = dict["title"] as! String
- let content = dict["content"] as! String
- let id = dict["id"] as! String
- print("ios======================\(id))")
- self.idDict[title] = id
- let point = QPointAnnotation.init()
- point.coordinate = CLLocationCoordinate2DMake(Double.init(lat)!, Double.init(lng)!)
- point.title = title
- point.subtitle = content
- self.txView.addAnnotation(point)
- }
- }
- }
- }
-
- if(args is NSDictionary){
- let dict = args as! NSDictionary
- let type = dict["type"] as! Int
- if type==2 {
- txView = QMapView.init(frame: frame)
- txView.delegate = self
- txView.showsUserLocation = true
- search = QMSSearcher.init(delegate: self)
- }
- locationManager = TencentLBSLocationManager.init()
- self.locationManager.apiKey = "OOZBZ-NHIWS-OCAOO-6VB3W-ROSBH-ZYB7Z"
- self.locationManager.delegate = self
- self.locationManager.pausesLocationUpdatesAutomatically = false
- self.locationManager.requestLevel = TencentLBSRequestLevel.adminName
- let authorizationStatus = CLLocationManager.authorizationStatus()
- if authorizationStatus != CLAuthorizationStatus.restricted {
- self.locationManager.requestWhenInUseAuthorization()
- self.locationManager.requestAlwaysAuthorization()
- }
- startSingleLocation();
- }
- }
-
- func mapView(_ mapView: QMapView!, viewFor overlay: QOverlay!) -> QOverlayView! {
- if overlay .isKind(of: QPolyline.self) {
- let polylineRender = QPolylineView.init(polyline: overlay as? QPolyline)!
- polylineRender.lineWidth = 11
- polylineRender.strokeColor = UIColor.orange
- return polylineRender
- }
- return nil
- }
-
- func startSingleLocation(){
- self.locationManager.requestLocation { (location, error) in
- let latitude = location?.location.coordinate.latitude ?? 40
- let longitude = location?.location.coordinate.longitude ?? 116
- if self.txView != nil{
- self.txView.setCenter(CLLocationCoordinate2DMake(latitude, longitude),animated: true)
- }
- self.methodChannel.invokeMethod("city", arguments: location?.city)
- self.methodChannel.invokeMethod("coordinate", arguments: "\(String(describing: latitude)),\(String(describing:longitude))")
- self.methodChannel.invokeMethod("address", arguments: ["lat":"\(latitude)","lng":"\(longitude)","title":location?.name as Any,"content":location?.address as Any,"airplaneName":location?.address as Any])
- }
- }
-
- func tencentLBSLocationManager(_ manager: TencentLBSLocationManager, didChange status: CLAuthorizationStatus) {
- startSingleLocation();
- }
-
- func mapView(_ mapView: QMapView!, viewFor annotation: QAnnotation!) -> QAnnotationView! {
- if annotation .isKind(of: QPointAnnotation.self) {
- let annotationIdentifier = "pointAnnotation"
- var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) ?? nil
- if pinView == nil {
- pinView = QPinAnnotationView.init(annotation: annotation, reuseIdentifier: annotationIdentifier)
- pinView?.canShowCallout = true
- pinView?.image = UIImage.init(named: "fj")
- }
- return pinView
- }
- return nil
- }
-
- func mapView(_ mapView: QMapView!, didTapAt coordinate: CLLocationCoordinate2D) {
- if self.points.count == 0 {
- print(coordinate)
- let revGeoOption = QMSReverseGeoCodeSearchOption.init()
- revGeoOption.setLocationWithCenter(coordinate)
- revGeoOption.poi_options = "address_format=short;radius=1000;page_size=20;page_index=1;policy=1;category=广场,大学,公交站,酒店"
- revGeoOption.get_poi = true
- self.search.search(with: revGeoOption)
- }
- }
-
- func mapView(_ mapView: QMapView!, didSelect view: QAnnotationView!) {
- let poi = view.annotation!
- if startLatLng != nil {
- if poi.coordinate.latitude != startLatLng.latitude || poi.coordinate.longitude != startLatLng.longitude {
- let title = poi.title?()
- let subtitle = poi.subtitle?()
- let id = self.idDict[title!]
- let map = ["lat":poi.coordinate.latitude,"lng":poi.coordinate.longitude,"title":title!,"content":subtitle!,"id":id!] as [String : Any]
- self.methodChannel.invokeMethod("endPoint", arguments: map)
- }
- }
- }
-
- func search(with reverseGeoCodeSearchOption: QMSReverseGeoCodeSearchOption, didReceive reverseGeoCodeSearchResult: QMSReverseGeoCodeSearchResult) {
- print(reverseGeoCodeSearchResult)
- let array = NSMutableArray()
- if reverseGeoCodeSearchResult.poi_count > 0 {
- reverseGeoCodeSearchResult.poisArray.forEach { (value) in
- let poi = value as! QMSReGeoCodePoi
- let dict = ["title":poi.title!,"content":poi.address!,"lat":"\(poi.location.latitude)","lng":"\(poi.location.longitude)"] as [String : Any]
- array.add(dict)
- }
- }else{
- let dict = ["title":"未知区域","content":"所选位置未获取到任何位置信息","lat":"\(reverseGeoCodeSearchResult.ad_info.location.latitude)","lng":"\(reverseGeoCodeSearchResult.ad_info.location.longitude)"] as [String : Any]
- array.add(dict)
- }
- self.methodChannel.invokeMethod("search", arguments: array)
- }
- func search(with searchOption: QMSSearchOption, didFailWithError error: Error) {
- print(error)
- }
-
- func view() -> UIView {
- return txView ?? UIView()
- }
-
- func tencentLBSLocationManager(_ manager: TencentLBSLocationManager, didFailWithError error: Error) {
- NSLog("error=========================================================>>\(error)")
- }
-
- deinit {
- self.methodChannel.setMethodCallHandler(nil)
- }
-
- }
|