以下代码是关于堆栈溢出的相同问题。然而,看起来代码是在swift 2中。我为一些代码添加了一个图像jj,但我收到1条错误消息。错误消息为var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
。错误消息指出 reuseId是未解析的标识符这是唯一的错误消息。如果修复了,代码将编译。
import UIKit
import MapKit
class Annotation: NSObject, MKAnnotation
{
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
var custom_image: Bool = true
var color: MKPinAnnotationColor = MKPinAnnotationColor.purple
}
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var thisMAP: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.thisMAP.delegate = self;
let annotation = Annotation()
thisMAP.addAnnotation(annotation)
let annotation2 = Annotation()
annotation2.coordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 1.0)
annotation2.custom_image = false
thisMAP.addAnnotation(annotation2)
let annotation3 = Annotation()
annotation3.coordinate = CLLocationCoordinate2D(latitude: 1.0, longitude: 0.0)
annotation3.custom_image = false
annotation3.color = MKPinAnnotationColor.green
thisMAP.addAnnotation(annotation3)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
return nil
}
var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
if anView == nil {
if let anAnnotation = annotation as? Annotation {
if anAnnotation.custom_image {
let reuseId = "jj.png"
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.image = UIImage(named:"jj.png")
}
else {
let reuseId = "pin"
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView.pinColor = anAnnotation.color
anView = pinView
}
}
anView.canShowCallout = false
}
else {
anView.annotation = annotation
答案 0 :(得分:0)
在这一行中,在这个地方不知道reuseID - 只在if-else
块内,这就是为什么它是错误reuseId is a unresolved identifier
:
var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
您只能在定义它的范围内使用它。
将reuseID
的定义放在此行之前!
它应与您在reuseId
if anAnnotation.custom_image {
相同