微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 在swift 2.1中使用MapKit添加不同的引脚颜色

我是 Swift的新手.我正在尝试在特定引脚上使用不同颜色的引脚或自定义引脚.我的代码有效.我有一个紫色的针,但我希望它们之间有所区别.我该怎么做?我认为在MapView委托方法中有一些事情要做,但我没有找到它.
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协议的自定义注释类(一种简单的方法是子类化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属性设置视图的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
}

原文地址:https://www.jb51.cc/iOS/335615.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐