广东话设置自定义图钉图像在图形页面,斯威夫特3
问题描述:
迷你地图代码:广东话设置自定义图钉图像在图形页面,斯威夫特3
import UIKit
import MapKit
@IBOutlet weak var ProfileMapView: MKMapView!
func mapView(_ ProfileMapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isMember(of: MKUserLocation.self) {
return nil
}
let reuseId = "ProfilePinView"
var pinView = ProfileMapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
}
pinView!.canShowCallout = true
pinView!.image = UIImage(named: "CustomPinImage")
return pinView
}
override func viewDidLoad() {
super.viewDidLoad()
let LightHouseLocation = CoordinatesTemplate
// Drop a pin
let lightHouse = MKPointAnnotation()
lightHouse.coordinate = LightHouseLocation
lightHouse.title = barNameTemplate
lightHouse.subtitle = streetNameTemplate
ProfileMapView.addAnnotation(lightHouse)
}
为什么不工作?为什么我无法让自定义图像像针图一样工作?它适用于另一个视图控制器。 感谢提前
答
你的问题是缺少you're添加UIViewController
为您MKMapView
的委托,所以你需要在你的viewDidLoad
加入这一行,因为我在我的意见
self.ProfileMapView.delegate = self
说我还建议你使用扩展模式,使你的代码更易读
extension YourViewController : MKMapViewDelegate{
func mapView(_ ProfileMapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isMember(of: MKUserLocation.self) {
return nil
}
let reuseId = "ProfilePinView"
var pinView = ProfileMapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)}
pinView!.canShowCallout = true
pinView!.image = UIImage(named: "CustomPinImage")
return pinView
}
}
你设置你的viewController作为地图的委托? –
这是什么意思? – RandomGeek
加入您的viewDidLoad此行'self.ProfileMapView.delegate = self' –