gpt4 book ai didi

ios - Swift 3 - MKPointAnnotation 自定义图像

转载 作者:搜寻专家 更新时间:2023-10-30 22:05:23 27 4
gpt4 key购买 nike

这是我的代码,我想添加一个自定义图钉(.png 文件)而不是红色图钉。我尝试使用 MKPinAnnotationView 和 MKAnnotationView,但无法添加坐标、字幕和标题。我是 iOS 开发新手。

override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
commentTextField.delegate = self

coreLocationManager.delegate = self
//desired accuracy is the best accuracy, very accurate data for the location
coreLocationManager.desiredAccuracy = kCLLocationAccuracyBest
//request authorization from the user when user using my app
coreLocationManager.requestWhenInUseAuthorization()

coreLocationManager.startUpdatingLocation()

dbRef = FIRDatabase.database().reference()

struct Location {
let title: String
let latitude: Double
let longitude: Double
let subtitle: String
}
// Locations array
let locations = [
Location(title: "Dio Con Dio", latitude: 40.590130, longitude: 23.036610,subtitle: "cafe"),
Location(title: "Paradosiako - Panorama", latitude: 40.590102, longitude: 23.036180,subtitle: "cafe"),
Location(title: "Veranda", latitude: 40.607740, longitude: 23.103044,subtitle: "cafe")
]

for location in locations {
let annotation = MKPointAnnotation()

annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
annotation.subtitle = location.subtitle

map.addAnnotation(annotation)
}



}

最佳答案

您需要将您的 View Controller 指定为 map View 的委托(delegate)(在 IB 中或以编程方式在 viewDidLoad 中,然后 (a) 指定您符合 MKMapViewDelegate 协议(protocol);和 (b) 实现 mapView(_:viewFor:) :

extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"

if annotation is MKUserLocation {
return nil
}

var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
annotationView?.image = UIImage(named: "custom_pin.png")

// if you want a disclosure button, you'd might do something like:
//
// let detailButton = UIButton(type: .detailDisclosure)
// annotationView?.rightCalloutAccessoryView = detailButton
} else {
annotationView?.annotation = annotation
}

return annotationView
}
}

有关详细信息,请参阅 Location and Maps Programming Guide: Creating Annotation Views from Your Delegate Object .代码片段在 Objective-C 中,但它描述了基本过程。

关于ios - Swift 3 - MKPointAnnotation 自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43877547/

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