MapAmapMarkerWriterCenterView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // MapAmapMarkerWriterCenterView.swift
  3. // map_amap_ios
  4. //
  5. // Created by 诺诺诺的言 on 2025/7/22.
  6. //
  7. import Foundation
  8. //import MapKit
  9. import MAMapKit
  10. class MapAnnotationAnchorPointView: MAPinAnnotationView {
  11. static let identifier: String = "MapAnnotationAnchorPointView"
  12. var marker: ATMapMarker? {
  13. didSet {
  14. updateView()
  15. }
  16. }
  17. var centerContentView: UIView?
  18. override init!(annotation: (any MAAnnotation)!, reuseIdentifier: String!) {
  19. super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
  20. self.marker = annotation as? ATMapMarker
  21. }
  22. // override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
  23. // super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
  24. // self.backgroundColor = UIColor.purple
  25. // self.marker = annotation as? ATMapMarker
  26. //// self.isEnabled = marker?.markerType.supportSelected ?? false
  27. //// self.isHidden = false
  28. // }
  29. required init?(coder aDecoder: NSCoder) {
  30. fatalError("init(coder:) has not been implemented")
  31. }
  32. // MARK: - 视图更新与重用
  33. override func prepareForReuse() {
  34. super.prepareForReuse()
  35. // 清除当前所有子视图
  36. for subview in subviews {
  37. subview.removeFromSuperview()
  38. }
  39. }
  40. private func updateView() {
  41. // 确保在更新视图前移除所有子视图
  42. for subview in subviews {
  43. subview.removeFromSuperview()
  44. }
  45. guard marker != nil else { return }
  46. let supContentView = UIView();
  47. supContentView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
  48. supContentView.backgroundColor = UIColor.clear
  49. addSubview(supContentView)
  50. if (marker?.markerType.isTracePassing ?? false) {
  51. let whiteView = UIView(frame: CGRect(x: 0, y: 0, width: 18, height: 18))
  52. whiteView.center = supContentView.center;
  53. whiteView.backgroundColor = UIColor.white
  54. whiteView.layer.cornerRadius = 9
  55. whiteView.layer.masksToBounds = true
  56. supContentView.addSubview(whiteView)
  57. //supContentView.addSubview(whiteView)
  58. centerContentView = UIView();
  59. centerContentView?.frame = CGRect(x: 2, y: 2, width: 14, height: 14)
  60. centerContentView?.backgroundColor = UIColor(hex: "#15CBA1") //UIColor(hex: "#15CBA1")
  61. centerContentView?.layer.cornerRadius = 7
  62. centerContentView?.layer.masksToBounds = true // 裁剪超出圆角部分的内容
  63. whiteView.addSubview(centerContentView ?? UIView())
  64. } else {
  65. let errorImage = UIImageView(frame: supContentView.bounds)
  66. errorImage.contentMode = .scaleAspectFit
  67. errorImage.image = readImageContentFrom(imageName: "com.shishi.dingwei_location_error.png")
  68. //errorImage.translatesAutoresizingMaskIntoConstraints = false
  69. supContentView.addSubview(errorImage)
  70. }
  71. supContentView.translatesAutoresizingMaskIntoConstraints = false
  72. NSLayoutConstraint.activate([
  73. supContentView.centerXAnchor.constraint(equalTo: centerXAnchor),
  74. supContentView.centerYAnchor.constraint(equalTo: centerYAnchor),
  75. supContentView.widthAnchor.constraint(equalToConstant: 30),
  76. supContentView.heightAnchor.constraint(equalToConstant: 30)
  77. ])
  78. }
  79. private func readImageContentFrom(imageName : String) -> UIImage {
  80. if let image = UIImage(named: imageName) {
  81. return image
  82. }
  83. // 尝试从插件资源束加载
  84. let bundleURL = Bundle(for: MapAnnotationView.self).url(forResource: "map_amap_ios_image_source", withExtension: "bundle")
  85. print("bundleURLsfsfdsdf-----\(bundleURL)")
  86. if let bundleURL = bundleURL, let resourceBundle = Bundle(url: bundleURL) {
  87. return UIImage(named: imageName, in: resourceBundle, compatibleWith: nil) ?? UIImage()
  88. }
  89. return UIImage()
  90. }
  91. // MARK: - 更新视图
  92. override func layoutSubviews() {
  93. super.layoutSubviews()
  94. print("layoutSubviewssfsfsdf----");
  95. updateCenterOffset()
  96. }
  97. private func updateCenterOffset() {
  98. let markerSize = CGSize(width: 30, height: 30)
  99. // 没有文本时,仅需要将图片底部对准坐标点
  100. centerOffset = CGPoint(x: 0, y: -markerSize.height/2)
  101. }
  102. }