2017-03-08 11 views
0

iOS用アプリケーションの開発途中にあり、マップビューでカスタムアノテーションの問題が発生しました。ここで注釈の追加/削除時の奇妙なカスタムアノテーションの動作

はアイデアです:

1)私は、私が関心のあるすべての近くのポイントを取得するために私のバックエンドサーバーに要求を送信し、この場所から、マップにユーザーの場所にロードします。

2)以下のように私は、カスタムアノテーションを持っている:

class CustomPointAnnotation: MKPointAnnotation { 
    var imageName: String? 
    var activeImageName : String? 
    var trainCrossingId : Int? 
    var notificationCount : UILabel = UILabel() 
    var labelIsHidden : Bool = true 
} 

私はマップ上に注釈を追加するとき、私は動的に私は私のFirebaseデータベースから取得したデータを使用するようにnotificationCountラベルを設定しています。

3)ユーザーは、現在の位置を中心に半径のサイズを増減できます。この新しい半径を選択して、mapView.removeAnnotations(mapView.annotations)を使用してすべての注釈を削除し、選択した半径を使用してサーバーから取得した新しいデータでこれらの注釈を再追加します。

問題:最初のビューをロードするときに設定正しいラベルと予想されるように、注釈等

作業している

しかし、一度ユーザが半径を更新し、Iは、追加/削除注釈とそれに対応するラベルが期待通りに表示されません。

例えば

が、これは、マップが最初に視界に入ったときにどのように見えるかです:

enter image description here

そして私は、半径の大きさ変更後: enter image description here

予想されるデータを最初の画像に表示されているもの(最初にビューに入ったとき)ですが、半径が更新されると、注釈のZ値は尊重されず、notificationCountは正しくありません(たとえば、ラベルを持たない)。これは私がfunc mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {...}の各注釈を監視するために印刷ステートメントとブレークポイントを設定していて、値が正しいと思いますが、ビューにはこれらの値が表示されていないので変です。

ここで間違っている可能性のあるアイデアはありますか?前もって感謝します!

編集には、以下のMapViewデリゲートを含めます

// Custom annotation view 
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

     if !(annotation is CustomPointAnnotation) { 
      return nil 
     } 

     let reuseId = "trainPin" 

     var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 
     if anView == nil { 
      anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     } 
     else { 
      anView?.annotation = annotation 
     } 

     let cpa = annotation as! CustomPointAnnotation 
     let icon : UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 32, height: 32)) 
     icon.image = UIImage(named:cpa.imageName) 
     icon.layer.zPosition = 1 
     anView?.rightCalloutAccessoryView = cpa.annotationButton 
     anView?.addSubview(cpa.notificationCount) 
     anView?.addSubview(icon) 
     anView?.frame = CGRect(x: 0, y:0, width:32, height:32) 
     anView?.canShowCallout = true 
     return anView 
    } 
+0

あなたは' FUNCののMapView(MKAnnotation:MKMapView、viewFor注釈_のMapView)のためにあなたのコードを投稿することができます:再び追加し、最初のサブビューを削除するようにしてください? – chengsam

+0

もちろん、元の質問に追加するように編集しました。ありがとう@chengsam – Dayna

答えて

1

私は再利用されますanViewとして削除や注釈を追加すると、ビューは多くのサブビューを持っていると確信しています。 > MKAnnotationView ` - ?

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if !(annotation is CustomPointAnnotation) { 
     return nil 
    } 

    let reuseId = "trainPin" 

    var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 
    if anView == nil { 
     anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
    } 
    else { 
     anView?.annotation = annotation 
    } 

    let cpa = annotation as! CustomPointAnnotation 
    let icon : UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 32, height: 32)) 
    icon.image = UIImage(named:cpa.imageName) 
    icon.layer.zPosition = 1 
    anView?.rightCalloutAccessoryView = cpa.annotationButton 
    for view in anView?.subviews { 
     view.removeFromSuperView() 
    } 
    anView?.addSubview(cpa.notificationCount) 
    anView?.addSubview(icon) 
    anView?.frame = CGRect(x: 0, y:0, width:32, height:32) 
    anView?.canShowCallout = true 
    return anView 
} 
+0

これはトリックでした!誠にありがとうございます、私は非常にあなたの助けに感謝 - 私は、サブビューの問題に巻き込まれたとは思わない! – Dayna

+0

@Daynaこれまでのコメントでは、これはうまくいかないと言われました。あなたは何か違うことをしましたか? – chengsam

+0

は私のせいで、正しくビルドしなかったので、コードは更新されませんでした。再構築され、それは今働いています - もう一度感謝、あなたは時間のトンを保存しました:) – Dayna