| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //
- // MapAmapMarkerWriterCenterView.swift
- // map_amap_ios
- //
- // Created by 诺诺诺的言 on 2025/7/22.
- //
- import Foundation
- //import MapKit
- import MAMapKit
- class MapAnnotationAnchorPointView: MAPinAnnotationView {
-
- static let identifier: String = "MapAnnotationAnchorPointView"
- var marker: ATMapMarker? {
- didSet {
- updateView()
- }
- }
- var centerContentView: UIView?
-
- override init!(annotation: (any MAAnnotation)!, reuseIdentifier: String!) {
- super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
- self.marker = annotation as? ATMapMarker
- }
-
- // override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
- // super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
- // self.backgroundColor = UIColor.purple
- // self.marker = annotation as? ATMapMarker
- //// self.isEnabled = marker?.markerType.supportSelected ?? false
- //// self.isHidden = false
- // }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- // MARK: - 视图更新与重用
-
- override func prepareForReuse() {
- super.prepareForReuse()
- // 清除当前所有子视图
- for subview in subviews {
- subview.removeFromSuperview()
- }
- }
-
- private func updateView() {
- // 确保在更新视图前移除所有子视图
- for subview in subviews {
- subview.removeFromSuperview()
- }
-
-
- guard marker != nil else { return }
-
- let supContentView = UIView();
- supContentView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
- supContentView.backgroundColor = UIColor.clear
- addSubview(supContentView)
-
- if (marker?.markerType.isTracePassing ?? false) {
- let whiteView = UIView(frame: CGRect(x: 0, y: 0, width: 18, height: 18))
- whiteView.center = supContentView.center;
- whiteView.backgroundColor = UIColor.white
- whiteView.layer.cornerRadius = 9
- whiteView.layer.masksToBounds = true
- supContentView.addSubview(whiteView)
- //supContentView.addSubview(whiteView)
-
- centerContentView = UIView();
- centerContentView?.frame = CGRect(x: 2, y: 2, width: 14, height: 14)
- centerContentView?.backgroundColor = UIColor(hex: "#15CBA1") //UIColor(hex: "#15CBA1")
- centerContentView?.layer.cornerRadius = 7
- centerContentView?.layer.masksToBounds = true // 裁剪超出圆角部分的内容
- whiteView.addSubview(centerContentView ?? UIView())
- } else {
- let errorImage = UIImageView(frame: supContentView.bounds)
- errorImage.contentMode = .scaleAspectFit
- errorImage.image = readImageContentFrom(imageName: "com.shishi.dingwei_location_error.png")
- //errorImage.translatesAutoresizingMaskIntoConstraints = false
- supContentView.addSubview(errorImage)
- }
-
- supContentView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- supContentView.centerXAnchor.constraint(equalTo: centerXAnchor),
- supContentView.centerYAnchor.constraint(equalTo: centerYAnchor),
- supContentView.widthAnchor.constraint(equalToConstant: 30),
- supContentView.heightAnchor.constraint(equalToConstant: 30)
- ])
- }
-
-
- private func readImageContentFrom(imageName : String) -> UIImage {
- if let image = UIImage(named: imageName) {
- return image
- }
-
- // 尝试从插件资源束加载
- let bundleURL = Bundle(for: MapAmapMarkerNormalView.self).url(forResource: "map_amap_ios_image_source", withExtension: "bundle")
- if let bundleURL = bundleURL, let resourceBundle = Bundle(url: bundleURL) {
- return UIImage(named: imageName, in: resourceBundle, compatibleWith: nil) ?? UIImage()
- }
-
- return UIImage()
- }
-
-
- // MARK: - 更新视图
-
- override func layoutSubviews() {
- super.layoutSubviews()
- print("layoutSubviewssfsfsdf----");
- updateCenterOffset()
- }
-
- private func updateCenterOffset() {
- let markerSize = CGSize(width: 30, height: 30)
- // 没有文本时,仅需要将图片底部对准坐标点
- centerOffset = CGPoint(x: 0, y: -markerSize.height/2)
- }
-
- }
|