ios – 在Swift中更改MKMapView上的图钉图像

我试图在 Swift中改变MKMapView上的图像,但遗憾的是它不起作用.任何想法我做错了什么?我在这里看到了一些例子,但没有奏效.
import UIKit
import MapKit

class AlarmMapViewController: UIViewController {
    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        showalarms()
        map.showsUserLocation = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func showalarms(){

        map.region.center.latitude = 49
        map.region.center.longitude = 12
        map.region.span.latitudeDelta = 1
        map.region.span.longitudeDelta = 1

        for alarm in Alarms.sharedInstance.alarms {

            let location = CLLocationCoordinate2D(
                latitude: Double(alarm.latitude),longitude: Double(alarm.longtitude)
            )

            let annotation = MKPointAnnotation()
            annotation.setCoordinate(location)
            annotation.title = alarm.name
            annotation.subtitle = alarm.description
            mapView(map,viewForAnnotation: annotation).annotation = annotation
            map.addAnnotation(annotation)
        }
    }

    @IBAction func zoomIn(sender: AnyObject) {
    }

    @IBAction func changeMapType(sender: AnyObject) {
    }

    func mapView(mapView: MKMapView!,viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.image = UIImage(named:"GreenDot")!

        }
        else {
            pinView!.annotation = annotation
        }

        return pinView
    }
}

GreenDot图片可在其他地方使用.

解决方法

别忘了设置:
map.delegate = self

并确保您的UIViewController实现MKMapViewDelegate协议.
如果您忘记执行此操作,则不会为您的地图调用mapView:viewForAnnotation:的实现.

此外,它看起来像pinView!.animatesDrop = true打破自定义图像.您必须将其设置为false,或使用MKAnnotationView(没有animatesDrop属性).

如果要实现自定义拖放动画,请参阅this related question.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...