Custom image as annotation pin with two different colour images
我正在尝试为我的图钉注释添加自定义图像,并为某些注释更改自定义图像的颜色。颜色变了。但是,图像不显示。而是显示默认引脚。
这是我的代码:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
class MyPointAnnotation : MKPointAnnotation {
var pinTintColor: UIColor? } class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{ func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier:”myAnnotation”) as? MKPinAnnotationView if annotationView == nil { if annotation is MKUserLocation { if let annotation = annotation as? MyPointAnnotation { return annotationView override func viewDidLoad() { let annotation1 = MyPointAnnotation() let annotation2 = MyPointAnnotation() |
图像”BLog.png” 位于主包中。
我已将 MKMapView 分配为委托。
但图像仍然不会改变。
您需要使用 MKAnnotationView 而不是 MKPinAnnotationView 为您的 pin 注释添加自定义图像。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil } if // Image pin // { let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure) |
来源:https://www.codenong.com/49164637/