gpt4 book ai didi

swift - 自定义注释 Swift

转载 作者:行者123 更新时间:2023-11-30 12:47:17 24 4
gpt4 key购买 nike

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

最佳答案

我认为它是您想要的默认引脚的不同图像?我已经使用了另一个问题的答案,请在这里查看 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 - 自定义注释 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41480737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com