|
|
@@ -13,17 +13,36 @@ class MapAnnotationView: MKAnnotationView {
|
|
|
static let identifier: String = "MapAnnotationView"
|
|
|
|
|
|
var marker: ATMapMarker?
|
|
|
+
|
|
|
+ var annotationSelected: Bool {
|
|
|
+ return (marker?.isSelected ?? false) || self.isSelected
|
|
|
+ }
|
|
|
+
|
|
|
private let imageView = UIImageView()
|
|
|
+ private var uiImage: UIImage? {
|
|
|
+ // 先尝试从主资源束加载
|
|
|
+ let imageName = marker?.markerType.imageName(selected: annotationSelected) ?? ""
|
|
|
+ print(imageName)
|
|
|
+ if let image = UIImage(named: imageName) {
|
|
|
+ return image
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试从插件资源束加载
|
|
|
+ let bundleURL = Bundle(for: MapAnnotationView.self).url(forResource: "map_mapkit_ios_resources", withExtension: "bundle")
|
|
|
+ if let bundleURL = bundleURL, let resourceBundle = Bundle(url: bundleURL) {
|
|
|
+ return UIImage(named: imageName, in: resourceBundle, compatibleWith: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ }
|
|
|
|
|
|
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
|
|
|
- super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
|
|
|
self.marker = annotation as? ATMapMarker
|
|
|
+ super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
|
|
|
|
|
|
- // 确保视图可见
|
|
|
- self.canShowCallout = false
|
|
|
- self.isEnabled = true
|
|
|
+ self.isEnabled = marker?.markerType.supportSelected ?? false
|
|
|
self.isHidden = false
|
|
|
-
|
|
|
+ // 添加图片显示
|
|
|
setupImageView()
|
|
|
}
|
|
|
|
|
|
@@ -33,15 +52,15 @@ class MapAnnotationView: MKAnnotationView {
|
|
|
|
|
|
private func setupImageView() {
|
|
|
// 设置默认图像
|
|
|
- let defaultImage = UIImage()
|
|
|
- imageView.image = defaultImage
|
|
|
+ imageView.image = uiImage
|
|
|
imageView.contentMode = .scaleAspectFit
|
|
|
|
|
|
// 添加图像视图到注释视图
|
|
|
addSubview(imageView)
|
|
|
|
|
|
// 设置图像视图的大小和位置
|
|
|
- imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
|
|
|
+ let size = marker?.markerType.size ?? .init()
|
|
|
+ imageView.frame = CGRect(origin: .init(), size: size)
|
|
|
|
|
|
// 设置注释视图的大小
|
|
|
self.frame = imageView.frame
|
|
|
@@ -53,22 +72,17 @@ class MapAnnotationView: MKAnnotationView {
|
|
|
override func prepareForDisplay() {
|
|
|
super.prepareForDisplay()
|
|
|
|
|
|
- // 根据标记状态更新图像
|
|
|
+ // 确保图像是最新的
|
|
|
updateImage()
|
|
|
}
|
|
|
|
|
|
private func updateImage() {
|
|
|
- // 使用已有的图像资源,而不是不存在的 marker_selected 和 marker_default
|
|
|
- let defaultImage = UIImage()
|
|
|
- imageView.image = defaultImage
|
|
|
+ let oldImage = imageView.image
|
|
|
+ imageView.image = uiImage
|
|
|
|
|
|
- // 如果被选中,可以修改图像视图的外观
|
|
|
- if let isSelected = marker?.isSelected, isSelected {
|
|
|
- imageView.layer.borderWidth = 2.0
|
|
|
- imageView.layer.borderColor = UIColor.blue.cgColor
|
|
|
- imageView.layer.cornerRadius = imageView.frame.width / 2
|
|
|
- } else {
|
|
|
- imageView.layer.borderWidth = 0
|
|
|
+ // 如果图像发生变化或为空,重新设置图像视图
|
|
|
+ if imageView.image == nil || oldImage != imageView.image {
|
|
|
+ setupImageView()
|
|
|
}
|
|
|
}
|
|
|
|