2017-08-24 4 views
2

カスタム注釈ビューを表示するのに苦労しています。具体的には、「ピン」という名前のイメージを新しいマップピンに設定しようとしています。デフォルトのピンは常に表示されます。私はpinをpin.pngに変更し、mapView:viewForメソッドの構造を変更するなど、わずかな時間を少しでも変更しました。ここに私が持っているものがあります。何か目立つかどうか見てみてください。カスタムMKAnnotationViewを作成できません

ありがとうございました!

注釈クラス:

class Annotation: NSObject, MKAnnotation { 
    dynamic var coordinate : CLLocationCoordinate2D 
    var title: String? 
    var subtitle: String? 

    init(location coord:CLLocationCoordinate2D) { 
     self.coordinate = coord 
     super.init() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

AnnotationViewクラス:

class AnnotationView : MKAnnotationView { 
    override init(annotation:MKAnnotation?, reuseIdentifier: String?) { 
     super.init(annotation: annotation, 
       reuseIdentifier: reuseIdentifier) 
     let im = UIImage(named: "pin")! 
     self.frame = CGRect(x: 0, y: 0, width: im.size.width/3.0 + 5, height: im.size.height/3.0 + 5) 
     self.centerOffset = CGPoint(x: 0, y: -20) 
     self.isOpaque = false 
    } 
    required init (coder: NSCoder) { 
     fatalError("NSCoding not supported") 
    } 
    override func draw(_ rect: CGRect) { 
     let im = UIImage(named: "pin")! 
     im.draw(in: self.bounds.insetBy(dx: 5, dy: 5)) 
    } 
} 

のMapView:viewFor:方法:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    var v : MKAnnotationView! = nil 
    let ident = "pin" 
    v = mapView.dequeueReusableAnnotationView(withIdentifier: ident) 
    if v == nil { 
     v = AnnotationView(annotation: annotation, reuseIdentifier: ident) 
     v.canShowCallout = true 
    } 
    v.annotation = annotation 
    return v 
} 

その他の関連するメソッド:

@IBAction func submitDog(_ sender: Any) { 
    let newDog = Dog(name: newDogName.text!, score: 11, picture: image!, location: location!) 
    dogs.append(newDog) 
    print(dogs.last!) 
    UIView.animate(withDuration: 0.5, animations: { 

    }) { _ in 
     self.newDogView.animation = "slideUp" 
     self.newDogView.animate() 
     self.newDogView.isHidden = true 
     self.newDogName.text = "" 
     self.map.isUserInteractionEnabled = true 
    } 
    dropNewPin(locatedAt: dogs.last!.location, name: dogs.last!.name, rate: dogs.last!.score) 
} 

func dropNewPin(locatedAt: CLLocation, name: String, rate: Int) { 
    let annotation = Annotation(location: CLLocationCoordinate2D(latitude: locatedAt.coordinate.latitude, longitude: locatedAt.coordinate.longitude)) 
    annotation.title = name 
    annotation.subtitle = "\(rate)/10" 
    self.map.addAnnotation(annotation) 
} 
+0

'self.mapView.delgate = self'を追加しましたか?私はその行を参照していません –

+0

私はあなたのコードが動作するが、これを行う正しい方法ではないの名前のピンと資産の画像を持っているかどうかを確認 –

+0

@ReinierMelianは、マップビューの代理人が働いて設定!ありがとうございました!しかし、予期しない動作が発生しました。現在の場所のデフォルト注釈も「ピン」画像として設定されています。 –

答えて

1

まず、あなたは、カスタム注釈ビューを必要とするならば、あなたが必要とする、私はあなたがMKAnnotationViewを使用する代わりに、カスタム描画した画像を修正し、追加をお勧めしますあなたのマップのデリゲートその後

self.mapView.delegate = self 

としてあなたのViewControllerを追加必要xibファイルとカスタムクラスをファイル所有者として追加して適切な調整を行う

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    //to avoid make a custom Annotation view for your user location 
    if(annotation is MKUserLocation){ 
     return nil 
    } 

    let ident = "pin" 
    var v = mapView.dequeueReusableAnnotationView(withIdentifier: ident) 
    if v == nil { 
     v = MKAnnotationView(annotation: annotation, reuseIdentifier: ident) 
     v?.image = UIImage(named: "pin") 
     v?.canShowCallout = true 
    } 
    v?.annotation = annotation 
    return v 
} 
+0

明確にするには、カスタムMKAnnotationViewクラスの必要はありませんか?私はカスタムMKAnnotationクラスを使用すべきですか?私の次のステップは、コールアウトをカスタマイズすることです。 –

+0

カスタムアノテーションビューのデザインが複雑すぎる場合は、カスタムアノテーションビュークラスを作成する必要がありますが、アノテーションデザインがイメージだけの場合は、@CodyPotterを行う必要はありません。コールアウトをカスタマイズすることは別の問題です。 MKAnnotationはプロトコルであり、実際にはどのクラスも座標プロパティを持つものしか実装できません –

関連する問題