【问题标题】:Add different pin color with MapKit in swift 2.1在 swift 2.1 中使用 MapKit 添加不同的引脚颜色
【发布时间】:2015-11-04 22:00:23
【问题描述】:

我是 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.
    }
    */

}

【问题讨论】:

    标签: ios swift mapkit swift2


    【解决方案1】:

    更好的方法是使用实​​现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
    }
    

    【讨论】:

    • 好的,非常感谢!我会试试这个,但我不明白一件事,我在哪里可以定义“resueID”?是一点的id??对不起我的英语不好我是法国人......再次感谢你的帮助:)
    • 您可以在viewForAnnotation 中定义reuseId :-) 例如let reuseId = "pin"
    • 这仅适用于iOS 9及以上,
    • @Shameerjan 在 iOS 9 之前,您可以将 pin 颜色更改为红色/绿色/紫色。pinView.pinColor = MKPinAnnotationColor.Purple 您无法更改其他颜色。
    • 我认为pinView!.canShowCallout = true 将是对 if 子句中 mapView 委托函数的一个很好的补充。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多