迷你地图代码:
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)
}为什么不动呢?为什么我不能让我的自定义图像作为引脚图像工作?它为我在另一个视图控制器上工作。先谢
发布于 2017-08-25 22:29:01
您的问题是,您缺少添加您的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
}
}https://stackoverflow.com/questions/45889861
复制相似问题