ios - 如何在 Mapview swift ios 中设置自定义图钉图像?

标签 ios swift3 mkmapview mkannotationview

@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
    map.delegate = self
    showAlarms()
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

func showAlarms(){

    map.region.center.latitude = 10.733051
    map.region.center.longitude = 76.763042
    map.region.span.latitudeDelta = 1
    map.region.span.longitudeDelta = 1


}
func mapView(mapView: MKMapView!,
             viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        //pinView!.animatesDrop = true
        pinView!.image = UIImage(named:"annotation")!

    }
    else {
        pinView!.annotation = annotation
    }

    return pinView
}

我需要在 swift ios 中显示我的 map View 的自定义图钉图像。自定义图像既不加载图钉。我有一个名为“注释”的图像,我需要将其加载到我的 map View 中。

最佳答案

  mapview.delegate = self

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
    return nil
}

 let annotationIdentifier = "Identifier"
 var annotationView: MKAnnotationView?
 if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
    annotationView = dequeuedAnnotationView
    annotationView?.annotation = annotation
}
else {
    annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
    annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}

 if let annotationView = annotationView {

    annotationView.canShowCallout = true
    annotationView.image = UIImage(named: "yourImagename”)
}
  return annotationView
}

关于ios - 如何在 Mapview swift ios 中设置自定义图钉图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41800302/

相关文章:

ios - 如何使用 NSUserDefaults 检查当前时间和上次发布时间?

ios - SwiftUI:将 View 与其父 View 的中心对齐

ios - UISearchBar iOS 返回索引超出范围的错误

ios - 无法检测到 webViewDidFinishLoad 事件

ios - iOS sdk 中是否提供当前位置/指南针标题按钮?

ios - 以 "generic"方式将 NSManagedObject 转换为结构体 (Swift)

ios - NSDictionary语法解释

swift - '...' 的重载存在这些结果类型 : ClosedRange<Bound>, CountableClosedRange<Bound>

ios - MKMapView on Storyboard on multiple Views with just 1 instance

ios - MKMapView 设置区域 : causes app to crash in iOS SDK