ios - 在 swift 2.1 中使用 MapKit 添加不同的图钉颜色

标签 ios swift mapkit swift2

我是 Swift 的新手。我正在尝试在特定图钉上使用不同颜色的图钉或自定义图钉。我的代码有效。我有一个紫色别针,但我想在它们之间有所作为。我该怎么做?我认为在 MapView 委托(delegate)方法中有一些事情要做,但我没有找到它。

import UIKit
import MapKit

class MapsViewController: UIViewController , MKMapViewDelegate{
    var shops: NSArray? {
        didSet{
            self.loadMaps()
        }
    }

    @IBOutlet weak var map: MKMapView?

    override func viewDidLoad() {
        super.viewDidLoad()
        loadMaps()
        self.title = "Carte"
        self.map!.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        // simple and inefficient example

        let annotationView = MKPinAnnotationView()

        annotationView.pinTintColor = UIColor.purpleColor()
        return annotationView
    }

    func loadMaps(){
//        navigationController?.navigationBar.topItem!.title = "Carte"
        let shopsArray = self.shops! as NSArray
        for shop in shopsArray  {

            let location = CLLocationCoordinate2D(
                latitude: shop["lat"] as! Double,
                longitude: shop["long"] as! Double
            )


            let annotation = MKPointAnnotation()
            annotation.coordinate   = location
            annotation.title        = shop["name"] as? String
            annotation.subtitle     = shop["addresse"] as? String

            map?.addAnnotation(annotation)

        }





        // add point



    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

最佳答案

更好的方法是使用实​​现MKAnnotation 协议(protocol)的自定义注释类(一种简单的方法是继承MKPointAnnotation)并添加任何需要的属性帮助实现自定义逻辑。

在自定义类中,添加一个属性,比如 pinColor,您可以使用它来自定义注释的颜色。

此示例子类 MKPointAnnotation:

import UIKit
import MapKit

class ColorPointAnnotation: MKPointAnnotation {
    var pinColor: UIColor

    init(pinColor: UIColor) {
        self.pinColor = pinColor
        super.init()
    }
}

创建 ColorPointAnnotation 类型的注释并设置它们的 pinColor:

let annotation = ColorPointAnnotation(pinColor: UIColor.blueColor())
annotation.coordinate = coordinate
annotation.title = "title"
annotation.subtitle = "subtitle"
self.mapView.addAnnotation(annotation)

在 viewForAnnotation 中,使用 pinColor 属性设置 View 的 pinTintColor:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

        let colorPointAnnotation = annotation as! ColorPointAnnotation
        pinView?.pinTintColor = colorPointAnnotation.pinColor
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}

关于ios - 在 swift 2.1 中使用 MapKit 添加不同的图钉颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33532883/

相关文章:

ios - didUpdateToLocation 从未调用过

iphone - 了解使用iTunes在多个iOS设备之间进行本地核心数据同步

ios - 在导航 Controller 内的 UIViewController 中设置背景

iOS Mapkit - 如何过滤注释(显示/隐藏)?

ios - 如何防止 uicollectionview 的单元格重用

ios - 如何将图像添加到 PFTableViewCell Swift 2 Xcode 7

ios - "[VKDefault] Tile in current unloaded state"iOS 11 map 不工作

ios - EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT,subcode=0xe7ffdefe) 发布构建问题

iOS 重用表格单元格设计

iphone - 发送文本然后调用联系人 iPhone 应用程序