Swift:MapKit自定义图像标注

4

在我的应用程序中,我有一个地图工具包和带有不同颜色的注释。但是,只有三种颜色选项(红色,绿色,紫色)。因此,我需要使用自定义图像更改注释。

我已经按照这个教程创建了我的地图视图

现在,我有一个Artwork类:

import Foundation
import MapKit

class Artwork: NSObject, MKAnnotation {
    let title: String
    let locationName: String
    let color: String
    let coordinate: CLLocationCoordinate2D

    init(title: String, locationName: String, color: String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.locationName = locationName
        self.color = color
        self.coordinate = coordinate

        super.init()
    }

    var subtitle: String {
        return locationName
    }

    // pinColor for disciplines: Sculpture, Plaque, Mural, Monument, other
    func pinColor() -> MKPinAnnotationColor  {
        switch color {
        case "Red":
            return .Red
        case "Purple":
            return .Purple
        case "Green":
            return .Green
        default:
            return .Green
        }
    }
}

此外,VCMapView.swift文件:
import Foundation
import MapKit

extension MapViewController: MKMapViewDelegate {

    // 1
    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if let annotation = annotation as? Artwork {
            let identifier = "pin"
            var view: MKPinAnnotationView
            if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
                as? MKPinAnnotationView { // 2
                    dequeuedView.annotation = annotation
                    view = dequeuedView
            } else {
                // 3
                view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                view.canShowCallout = true
                view.calloutOffset = CGPoint(x: -5, y: 5)
                view.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
            }
            view.pinColor = annotation.pinColor()
            return view
        }
        return nil
    }
}

我可以在viewdidload()中像这样添加我的地图标记:

// show artwork on map
let artwork = Artwork(title: "\(self.plate)",
locationName: "\(self.location)",
color: "\(self.color)",
coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longtitude))

self.mapView.addAnnotation(artwork)

所有工作都很完美,但需要向这个架构添加自定义图像,我不确定应该修改哪个部分。

1个回答

5
在我的看法中,你的架构应该在Artwork类中添加一个图像属性,然后在MKPinAnnotationView.image属性中使用id。
例如:
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
            }
view.image = UIImage(named:"your_image")
view.frame.size = CGSize(width: 30.0, height: 30.0) //not necessary but just to give you an idea how to resize just in case it is too large.

1
Nerkyator,你救了我的一天!我在喝啤酒庆祝你。我花了3个小时尝试更改我的标注图钉的大小。view.frame.size = CGSize(width: 30.0, height: 30.0) 在Swift 3中非常容易使用且有效。我尝试添加约束、view.sizeThatFits(width:, height:)等方法都无法调整我的图片大小,如果我手动调整图片大小则会影响图片质量。谢谢! - Brian Bird

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接