2016-10-24 8 views
1

enter image description hereautolayoutを使ってMKAnnotationに複数の行を表示する方法は?

私はmapkitを使用していますどのようにすることができMKAnnotationビューで複数の行。

すべての注釈にはタイトルとサブタイトルがあります。 どのように私はの自動レイアウトの助けを借りて複数の行のサブタイトルを表示する?

答えが見つかった.plz私の答えを試してください。私たちはちょうどコードを実行する必要があります

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 

} 

ここで私はMKAnnotationでautolayoutを使用する方法を示しています。

+0

可能な複製[MKAnnotationの字幕のテキストの2行を表示し、変更する方法右のボタンの画像?](http://stackoverflow.com/questions/5831382/how-to-display-2-lines-of-text-for-subtitle-of-mkannotation-and-change-the- image) – Jeyamahesan

+0

@Jeyamahesan私は、autolayoutをmkannotationでどのように使うべきかを知るためにこの質問を投稿します。 –

+0

既に、この回答に示されているのと同じ回答とコードスニペットで、同様のタイプの質問に回答しました(http://stackoverflow.com/a/39160132/6176024)。 – Jeyamahesan

答えて

5

我々はそれが非常に簡単です自動レイアウト

の助けを借りてMKAnnotationビューで複数のラインを表示することができます。スウィフト

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

     let identifier = "MyPin" 

     if annotation.isKindOfClass(MKUserLocation) { 
      return nil 
     } 

     var annotationView: MKPinAnnotationView? = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? MKPinAnnotationView 

     if annotationView == nil { 

      annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      annotationView?.canShowCallout = true 

       let label1 = UILabel(frame: CGRectMake(0, 0, 200, 21)) 
      label1.text = "Some text1 some text2 some text2 some text2 some text2 some text2 some text2" 
      label1.numberOfLines = 0 
       annotationView!.detailCalloutAccessoryView = label1; 

      let width = NSLayoutConstraint(item: label1, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200) 
      label1.addConstraint(width) 


      let height = NSLayoutConstraint(item: label1, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90) 
      label1.addConstraint(height) 



     } else { 
      annotationView!.annotation = annotation 
     } 
     return annotationView 
    } 


} 

enter image description here

について

客観Cで

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 

     if ([annotation isKindOfClass:[MKUserLocation class]]) 
      return nil; 
     if ([annotation isKindOfClass:[CustomAnnotation class]]) { 
      CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation; 

      MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"]; 

      if (annotationView == nil) 
       annotationView = customAnnotation.annotationView; 
      else 
       annotationView.annotation = annotation; 

      //Adding multiline subtitle code 

      UILabel *subTitlelbl = [[UILabel alloc]init]; 
      subTitlelbl.text = @"sri ganganagar this is my home twon.sri ganganagar this is my home twon.sri ganganagar this is my home twon. "; 

      annotationView.detailCalloutAccessoryView = subTitlelbl; 

      NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150]; 

      NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0]; 
      [subTitlelbl setNumberOfLines:0]; 
      [subTitlelbl addConstraint:width]; 
      [subTitlelbl addConstraint:height]; 




      return annotationView; 
     } else 
      return nil; 
    } 

出力

enter image description here

ここ

私はプログラム的にラベルを作成

NSLayoutConstraintを使用しています。その上に制約を追加し、ラベルをMKAnnotationViewのdetailCalloutAccessoryViewに追加します。使用

extension MKAnnotationView { 

    func loadCustomLines(customLines: [String]) { 
     let stackView = self.stackView() 
     for line in customLines { 
      let label = UILabel() 
      label.text = line 
      stackView.addArrangedSubview(label) 
     } 
     self.detailCalloutAccessoryView = stackView 
    } 



    private func stackView() -> UIStackView { 
     let stackView = UIStackView() 
     stackView.axis = .vertical 
     stackView.distribution = .fillEqually 
     stackView.alignment = .fill 
     return stackView 
    } 
} 

:複数行を追加するための

4

延長

view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
view.canShowCallout = true 
view.loadCustomLines(customLines: ["qqqq", "wwww", "eee"]) 
+0

すばらしい解決策! – hunter

0

をそして、あなたはマルチラベルがどこかに行くためにクリックさせたい場合は、する必要があります: の1-プログラムで作成AnnotationViewにUITapGestureRecognizerを作成しました。 2ジェスチャセレクタに渡す関数を作成します。この関数は、タイプUIGestureRecognizerの送信側パラメータを取ります。あなたが本当にcanshowcalloutを持っていることを確認するifステートメントを持っているので、実際のピン自体をクリックしないように表示されているときにクリックするだけです。

私の場合、私はこのシナリオを持っていました。これは誰かを助けるかもしれません: - 別の場所のセルが入っているtableView - 画面の上半分は、各場所のピンを示すマップです - マップ内のテーブルビューまたはピンをクリックすると、地図ピンの吹き出しビューを表示します。ピンの吹き出しビューは、テールを切り捨てるのを避けるために場所の名前が長い場合、画像と小さな折り返しの位置の名前が表示されます。 - クリックした場所を判断するためにコード内のどこにでもインデックスが渡され、使用されているため、クリックした場所のコールアウトに正しい名前を表示するためにもインデックスを使用する必要がありました。 私はannotationView.detailCalloutAccessoryView = myLabelを使っていたので、ラベルのクリックが問題でした。いつのように思える。右または左ではなく詳細を使用すると、controlTappedマップデリゲート関数からでもクリックを取得しません。だから私は前に話したジェスチャーセレクター関数の中で次のことをしなければならなかった: あなたが何らかのガードをしていることを確認してください。 - let tag = sender.view.tagを実行してビュータグを確認します。 - ビューをMKPinAnnotationViewとして取得します。view = sender.view as? MKPointAnnotationView - コールアウトが最初に表示されていることを確認してください。view.canShowCallout - ifまたはguardの中かっこの中にindex = tagを付けて、そのインデックスを関数に渡しますannotationViewをクリックした後、ネイティブリンゴマップに移動します。

  • はannotationForビューデリゲート関数内のすべてのことの後に私は、ジェスチャ認識セレクタにこの機能でパスの前に言ったよう
関連する問題