【问题标题】:mapKit not display annotationmapKit 不显示注释
【发布时间】:2017-01-24 22:00:12
【问题描述】:

我的代码没有显示每个图钉的标题或副标题。大头针出现在坐标上。但是,当单击引脚时,什么也没有出现。我只是希望别针是不同的颜色,当点击它们时会显示一条消息。

import UIKit
import MapKit

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.title = "My Shop"
    hello.subtitle = "its cool"
    hello.pinTintColor = .red


    let hellox = MyPointAnnotation()

    hellox.coordinate = CLLocationCoordinate2D(latitude: 34, longitude: -72)

    hellox.title = "NOW"

    hellox.title = "JUdo"
    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
    }}

【问题讨论】:

    标签: ios swift swift3 mapkit mkannotationview


    【解决方案1】:

    在返回annotationView之前添加annotationView?.canShowCallout = true

    【讨论】:

    • 我把它放在什么函数里。
    • @sillymilly - 显然,Grifas 正在谈论你的 mapView(_:viewFor:)。 (这是您返回 annotationView 的唯一函数。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多