如何在相同的注释视图下更改mapkit中的引脚颜色(swift3)

时间:2017-01-24 04:19:33

标签: ios swift swift3 mapkit mkannotationview

我在mapkit中有2个引脚,两个都在相同的注释视图下,所以它使得两个引脚都是相同的颜色。如何使引脚具有不同的颜色。我想你好红色和hellox是蓝色的。

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet var jmap: MKMapView!

override func viewDidLoad() {
    jmap.delegate = self;
    let hello = MKPointAnnotation()
    hello.coordinate = CLLocationCoordinate2D(latitude: 40, longitude: -73)
    jmap.addAnnotation(hello)
    let hellox = MKPointAnnotation()
    hellox.coordinate = CLLocationCoordinate2D(latitude: 34, longitude: -72)
    jmap.addAnnotation(hellox)
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = MKPinAnnotationView()
    annotationView.pinTintColor = .blue
    return annotationView
}}

1 个答案:

答案 0 :(得分:6)

子类pinTintColor添加您想要的任何自定义属性,例如class MyPointAnnotation : MKPointAnnotation { var pinTintColor: UIColor? } class ViewController: UIViewController, MKMapViewDelegate { @IBOutlet var jmap: MKMapView! override func viewDidLoad() { super.viewDidLoad() jmap.delegate = self let hello = MyPointAnnotation() hello.coordinate = CLLocationCoordinate2D(latitude: 40, longitude: -73) hello.pinTintColor = .red let hellox = MyPointAnnotation() hellox.coordinate = CLLocationCoordinate2D(latitude: 34, longitude: -72) hellox.pinTintColor = .blue jmap.addAnnotation(hello) jmap.addAnnotation(hellox) } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "myAnnotation") as? MKPinAnnotationView if annotationView == nil { annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation") } else { annotationView?.annotation = annotation } if let annotation = annotation as? MyPointAnnotation { annotationView?.pinTintColor = annotation.pinTintColor } return annotationView } }

idx=shuffle_range[batch_size*j+i]