【问题标题】:Custom Annotation Swift自定义注释 Swift
【发布时间】:2017-01-05 08:56:04
【问题描述】:

我创建了一个包含单个注释的地图。从我所见,这是一种非常简单的实现方式,我从教程中得到了。我目前正在尝试获取自定义图片作为注释,但我正在努力做到这一点,因为关于这个主题的几乎所有信息都在目标 C 中。如果它真的很简单,请原谅我,但我对编码相对较新,并会感激任何帮助:)

class ViewController2: UIViewController {
@IBOutlet var Map: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

//Location for first Pin
let locationOne = CLLocationCoordinate2DMake(-47.016945, 167.852095)

//Location for map centering
let locationNZ = CLLocationCoordinate2DMake(-43.937462, 170.507813)
let span = MKCoordinateSpanMake(9, 9)
let region = MKCoordinateRegion(center: locationNZ, span: span)
Map.setRegion(region, animated: true)

//Create annotation one
let annotation = MKPointAnnotation()
annotation.coordinate = locationOne
annotation.subtitle = "Park"
annotation.title = "Rakiura National Park"

//Add annotation to the map
Map.addAnnotation(annotation)}

【问题讨论】:

    标签: swift xcode annotations customization


    【解决方案1】:

    我假设它是您想要的默认图钉的不同图像? 我已经从另一个问题中使用了这个答案,请在此处查看 Custom pin image in annotationView in iOS

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    // Don't want to show a custom image if the annotation is the user's location.
    guard !annotation.isKindOfClass(MKUserLocation) else {
        return nil
    }
    
    let annotationIdentifier = "AnnotationIdentifier"
    
    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
        annotationView = av
    }
    
    if let annotationView = annotationView {
        // Configure your annotation view here
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "yourImage")
    }
    
    return annotationView
    }
    

    【讨论】:

    • 对不起,但我不是 Xcode 方面的佼佼者,并且有一个关于在答案中实现代码的问题。我将它放在右括号之前的类末尾,但是该函数从未使用过。它是在错误的位置还是我必须打电话给它?再次为我的无知感到抱歉。
    • 嗨尼克。您能否尝试编辑您的问题,以及代码现在的样子?
    【解决方案2】:

    更新@Jonask 的答案

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            // Don't want to show a custom image if the annotation is the user's location.
            guard !annotation.isKind(of: MKUserLocation.self) else {
                return nil
            }
    
            let annotationIdentifier = "AnnotationIdentifier"
    
            var annotationView: MKAnnotationView?
            if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
                annotationView = dequeuedAnnotationView
                annotationView?.annotation = annotation
            }
            else {
                let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
                av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
                annotationView = av
            }
    
            if let annotationView = annotationView {
                // Configure your annotation view here
                annotationView.canShowCallout = true
                annotationView.image = UIImage(named: "yourImage")
            }
    
            return annotationView
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多