MapAnnotationAnchorPointView.swift 4.2 KB

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