0
私はマップアノテーションの代わりに画像アノテーションを表示しようとしています。私はこのコードを使用しています:マップキットでカスタムアノテーションを表示
import UIKit
import MapKit
class ViewController2: UIViewController , MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)
let region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
mapView.setRegion(region, animated: true)
var info1 = CustomPointAnnotation()
info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)
info1.title = "Info1"
info1.subtitle = "Subtitle"
info1.imageName = "taxi"
var info2 = CustomPointAnnotation()
info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)
info2.title = "Info2"
info2.subtitle = "Subtitle"
info2.imageName = "smile"
mapView.addAnnotation(info1)
mapView.addAnnotation(info2)
mapView.showAnnotations(mapView.annotations, animated: true)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
print("delegate called")
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView?.canShowCallout = true
}
else {
anView?.annotation = annotation
}
//Set annotation-specific properties **AFTER**
//the view is dequeued or created...
let cpa = annotation as! CustomPointAnnotation
anView?.image = UIImage(named:cpa.imageName)
return anView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
}
}
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
}
私は、デリゲートはviewcontroller : mapView.delegate = self
との接続が、この方法では、viewForAnnotation
を呼び出していないと思います。
私はまだ地図上の代わりにcustominnotation
のピン注釈を参照してください。
の可能性のある重複[ iOS Swift MapKitカスタムアノテーション](http://stackoverflow.com/questions/38274115/ios-swift-mapkit-custom-annotation) –
ブレークポイントの設定とコードのトレースに精通している場合は、コードがいつviewForAnnotationデリゲートは "if!(注釈はCustomPointAnnotationです)"を渡すか、そこに戻ります。 –