在swift 2.1中使用MapKit添加不同的引脚颜色 [英] Add different pin color with MapKit in swift 2.1

查看:89
本文介绍了在swift 2.1中使用MapKit添加不同的引脚颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new in Swift. I'm trying to have different color pin or custom pin on specific pin. My code works. I've a purple pin, but I want make a difference between them. How can I do it? I think there something to do in MapView delegate method but I didn't find it.

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 )并添加帮助实现自定义逻辑所需的任何属性。

A better approach is to use a custom annotation class that implements the MKAnnotation protocol (an easy way to do that is to subclass MKPointAnnotation) and add whatever properties are needed to help implement the custom logic.

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

In the custom class, add a property, say pinColor, which you can use to customize the color of the annotation.

此示例子类MKPointAnnotation:

This example subclasses MKPointAnnotation:

import UIKit
import MapKit

class ColorPointAnnotation: MKPointAnnotation {
    var pinColor: UIColor

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

创建类型为 ColorPointAnnotation 的注释并设置其 pinColor

Create annotations of type ColorPointAnnotation and set their pinColor:

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

在viewForAnnotation中,使用 pinColor 属性设置视图的 pinTintColor

In viewForAnnotation, use the pinColor property to set the view's 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
}

这篇关于在swift 2.1中使用MapKit添加不同的引脚颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆