自定义注释Swift

时间:2017-01-05 08:56:04

标签: swift xcode annotations customization

我创建了一个包含单个注释的地图。从我所看到的,这是一个非常简单的实现方法,我得到了一个教程。我目前正在尝试将自定义图片作为注释,但我正在努力这样做,因为几乎关于此主题的所有信息都是客观的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)}

2 个答案:

答案 0 :(得分:0)

我假设你想要的默认引脚的图像不同? 我从另一个问题中使用了这个答案,请在这里查看 Custom pin image in annotationView in iOS

Cython

答案 1 :(得分:0)

更新@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
}