// // MapAmapThemeControl.swift // map_amap_ios // // Created by 诺诺诺的言 on 2025/7/22. // import Foundation //import MapKit import Combine import AMapFoundationKit import MAMapKit @available(iOS 13.0, *) class MapAmapThemeControl: UIViewController { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } let viewModel: MapAmapViewAndDataExchange var autonaviMap : MAMapView! //let mapView = MKMapView() private var cancellables = Set() init(viewModel: MapAmapViewAndDataExchange) { self.viewModel = viewModel super.init(nibName: nil, bundle: nil) //mapView.showsUserLocation = false } override func viewDidLoad() { super.viewDidLoad() AMapServices.shared().enableHTTPS = true loadMainMapView() bindViewModel() } private func loadMainMapView() { autonaviMap = MAMapView() autonaviMap.delegate = self autonaviMap.mapType = .standard autonaviMap.showsUserLocation = false autonaviMap.userTrackingMode = .follow autonaviMap.translatesAutoresizingMaskIntoConstraints = false view.addSubview(autonaviMap) NSLayoutConstraint.activate([ autonaviMap.topAnchor.constraint(equalTo: view.topAnchor), autonaviMap.bottomAnchor.constraint(equalTo: view.bottomAnchor), autonaviMap.leadingAnchor.constraint(equalTo: view.leadingAnchor), autonaviMap.trailingAnchor.constraint(equalTo: view.trailingAnchor) ]) } private func bindViewModel() { viewModel.$markers .receive(on: DispatchQueue.main) .sink { [weak self] markers in self?.autonaviMap.removeAnnotations(self?.autonaviMap.annotations) var mapMarkerList = [MAPointAnnotation](); for markerItem in markers { let annotationItem : MAPointAnnotation = MAPointAnnotation() annotationItem.title = markerItem.markerName; annotationItem.coordinate = markerItem.coordinate; mapMarkerList.append(annotationItem) } self?.autonaviMap.addAnnotations(mapMarkerList) self?.autonaviMap.selectAnnotation(mapMarkerList.first, animated: true) self?.autonaviMap.setZoomLevel(15.1, animated: true) let selectIemt : MAPointAnnotation = mapMarkerList.first! self?.autonaviMap.setCenter(selectIemt.coordinate, animated: true) //self?.mapView.removeAnnotations(self?.mapView.annotations ?? []) // UIView.animate(withDuration: 0.3) { // self?.mapView.addAnnotations(markers) // } } .store(in: &cancellables) /*viewModel.$currentRegion .compactMap { $0 } .receive(on: DispatchQueue.main) .sink { [weak self] region in self?.mapView.setRegion(region, animated: true) } .store(in: &cancellables) viewModel.$markers .receive(on: DispatchQueue.main) .sink { [weak self] markers in self?.mapView.removeAnnotations(self?.mapView.annotations ?? []) UIView.animate(withDuration: 0.3) { self?.mapView.addAnnotations(markers) } } .store(in: &cancellables) viewModel.$polylines .receive(on: DispatchQueue.main) .sink { [weak self] polylines in self?.mapView.removeOverlays(self?.mapView.overlays ?? []) var polyinesList = [MKPolyline](); for polyline in polylines { let polylineItem : MKPolyline = polyline.polyline polylineItem.associatedLineId = polyline.id polylineItem.associatedLineType = polyline.lineType polylineItem.associatedColor = polyline.color polylineItem.associatedWidth = polyline.width polyinesList.append(polylineItem) } self?.mapView.addOverlays(polyinesList) //self?.mapView.addOverlays(polylines.map({ $0.polyline })) if let padding = polylines.last?.mapPadding { self?.mapView.fitsAllPoints( points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [], padding: padding.padding, animated: true ) } } .store(in: &cancellables) //移动至多个点的位置,并提供设置padding距离 viewModel.$suitableLocation .receive(on: DispatchQueue.main) .sink{ [weak self] polylines in if let padding = polylines.last?.mapPadding { if let atMapPoint : [CLLocationCoordinate2D] = polylines.last?.points { self?.mapView.moveToSuitableLocation(points: atMapPoint, padding: padding) } } } .store(in: &cancellables)*/ } } @available(iOS 13.0, *) extension MapAmapThemeControl : MAMapViewDelegate { //根据anntation生成对应的View。 func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! { if annotation is MAPointAnnotation { let pointReuseIdentifier = "pointReuseIndetifier" var annotationView = mapView.dequeueReusableAnnotationView( withIdentifier: pointReuseIdentifier ) as? MAPinAnnotationView if annotationView == nil { annotationView = MAPinAnnotationView( annotation: annotation, reuseIdentifier: pointReuseIdentifier ) } annotationView?.canShowCallout = true annotationView?.animatesDrop = true annotationView?.isDraggable = false annotationView?.pinColor = .purple return annotationView } return nil } } /* @available(iOS 13.0, *) extension MapAmapThemeControl: MKMapViewDelegate { func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { guard let marker = annotation as? ATMapMarker else { return nil } print("markerTypesfdsdfs---\(marker.markerType.markType)"); if marker.markerType.isMapAchorPoint { let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationAnchorPointView.identifier) as? MapAnnotationAnchorPointView) ?? MapAnnotationAnchorPointView(annotation: marker, reuseIdentifier: MapAnnotationAnchorPointView.identifier) annotationView.marker = marker annotationView.prepareForDisplay() return annotationView } else if (marker.markerType.isTracePopup) { let annotationBubbleView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAmapPopUpWindowView.identifier) as? MapAmapPopUpWindowView) ?? MapAmapPopUpWindowView(annotation: marker, reuseIdentifier: MapAmapPopUpWindowView.identifier) annotationBubbleView.marker = marker annotationBubbleView.prepareForDisplay() return annotationBubbleView } let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationView.identifier) as? MapAnnotationView) ?? MapAnnotationView(annotation: marker, reuseIdentifier: MapAnnotationView.identifier) annotationView.marker = marker annotationView.prepareForDisplay() return annotationView } func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { guard var marker = view.annotation as? ATMapMarker else { return } viewModel.handleMarkerTap(marker: &marker) } func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { guard let polyline = overlay as? MKPolyline else { return MKOverlayRenderer(overlay: overlay) } let renderer = MapAmapArrowPolylineRenderer(polyline: polyline) renderer.lineWidth = 6 //rgba(123, 125, 255, 1) renderer.strokeColor = UIColor(red: 0.27, green: 0.46, blue: 1, alpha: 1)//UIColor.init(hex: "#4476FF");//UIColor(red: 123 / 255, green: 125 / 255, blue: 255 / 255, alpha: 1) renderer.lineCap = .round renderer.lineJoin = .round // 设置箭头样式 renderer.arrowColor = UIColor.white // 箭头颜色 renderer.arrowSize = 12 // 增大箭头大小 renderer.arrowSpacing = 50 // 减小箭头间距 renderer.borderWidth = 1 renderer.borderColor = UIColor(red: 0.35, green: 0.36, blue: 0.99, alpha: 1) //normal error selected color if polyline.associatedLineType == "normal" { renderer.strokeColor = UIColor(red: 0.27, green: 0.46, blue: 1, alpha: 1) } else if polyline.associatedLineType == "error" { renderer.strokeColor = UIColor(red: 1, green: 0.43, blue: 0.43, alpha: 1) } else if polyline.associatedLineType == "selected" { renderer.strokeColor = UIColor(red: 0.08, green: 0.8, blue: 0.63, alpha: 1) } else if polyline.associatedLineType == "color" { renderer.strokeColor = UIColor(hex: polyline.associatedColor ?? "#4476FF") renderer.lineWidth = polyline.associatedWidth ?? 0 } return renderer } }*/