1
我有兩個代碼塊之間的鏈接有問題,這將有望更改註釋圖像。我對swift很陌生,所以我很難在mapView上找到它。我在函數中給出了一個代碼,用於在運行時打印一個字符串,但該字符串未顯示在NSLog中。這是否意味着,該功能根本沒有運行?對於我出錯的地方有任何建議,我將不勝感激。謝謝類的佈局
class ViewController2: UIViewController {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
//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)
mapView.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
mapView.addAnnotation(annotation)}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
print("func")
guard !annotation.isKind(of: MKUserLocation.self) else {
return nil}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: 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 {
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "mapIcon.png")}
return annotationView}}
感謝您的回覆。試一試,但收到一個錯誤,說類型的值'(MKMapView,MKAnnotation) - > MKAnnotationView?'沒有會員代表。有關於此的任何想法? – imjonu
這沒有意義。它表明您正試圖將委託分配給一個功能。您將該委託分配給MapView對象上的委託屬性並將其指向自己。然後您實現MapViewDelegate協議。請參閱https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html –