自定义注释Swift [英] Custom Annotation Swift

查看:82
本文介绍了自定义注释Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含单个注释的地图.从我所看到的,这是实现此目标的非常简单的方法,我从教程中获得了这一方法.我目前正在尝试获取自定义图片作为注释,但由于与此主题有关的几乎所有信息都在目标C中,因此我正为此而努力.请原谅我,如果这样做确实很简单,但是我对编码还是比较陌生的,请多多关照帮助:)

I have created a map which contains a single annotation. From what I've seen, this is a very simple way of achieving this which I got off a tutorial. I am currently trying to get a custom picture as the annotation but am struggling to do so as almost all information on this topic is in objective C. Forgive me if it is really simple to do but I am relatively new to coding and would appreciate any help :)

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)}

推荐答案

我假设它与您想要的默认引脚的图像不同?我已经使用了另一个问题的答案,请在此处查看在iOS的批注视图中自定义图钉图像

I assume its a different image of the default pin you want? I've used this answer from another question, check it out here 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
}

这篇关于自定义注释Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆