2017-02-16 66 views
0

在我的應用程序,當用戶點擊一個註解爲我UITextviews顯示有關注釋如標題,緯度和經度一些更具描述性信息,我想一個視圖中顯示。我的問題是,我不知道在註釋添加到地圖後如何獲取這些信息。這裏是我用於MKAnnotationView的一些代碼,它只是內置到我的自定義視圖控制器中。獲取當前的註釋信息以在xamarin的iOS

MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) 
    { 
     MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(annotationIdentifier); 
     // Set current location and location of annotation 
     CLLocationCoordinate2D currentLocation = mapView.UserLocation.Coordinate; 
     CLLocationCoordinate2D annotationLocation = annotation.Coordinate; 

     // We don't want a special annotation for the user location 
     if (currentLocation.Latitude == annotationLocation.Latitude && currentLocation.Longitude == annotationLocation.Longitude) 
      return null; 

     if (annotationView == null) 
      annotationView = new MKPinAnnotationView(annotation, annotationIdentifier); 
     else 
      annotationView.Annotation = annotation; 

     annotationView.CanShowCallout = true; 
     (annotationView as MKPinAnnotationView).AnimatesDrop = false; // Set to true if you want to animate the pin dropping 
     (annotationView as MKPinAnnotationView).PinColor = MKPinAnnotationColor.Red; 
     annotationView.SetSelected(true, false); 

     _annotationDetailButton = UIButton.FromType(UIButtonType.DetailDisclosure); 
     _annotationDetailButton.TouchUpInside += (sender, e) => 
     { 
      //put create segue her 
      PerformSegue("ShowCollectionDetail", Self); 

     }; 

     annotationView.RightCalloutAccessoryView = _annotationDetailButton; 

     // Annotation icon may be specified like this, in case you want it. 
     // annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromBundle("example.png")); 
     return annotationView; 
    } 
+0

你看看我的答案? –

+0

我做了,我得到關於註釋的信息,但不是當前選擇的註釋。我將此代碼添加到GetViewForAnnotation方法。 SculptureTitle.Text = annotationView.Annotation.GetTitle();對於我仍然做錯的任何建議。看來我得到的註釋不在當前顯示。 – user3020229

回答

1

您可以創建自定義MKAnnotation如下:

public class AnnotationModel : MKAnnotation 
{ 
    private string _title; 
    private string _subtitle; 

    public AnnotationModel(CLLocationCoordinate2D coordinate, string title, string subtitle = "") 
    { 
     this.Coords = coordinate; 
     _title = title; 
     _subtitle = subtitle; 
    } 

    public override string Title { get { return _title; } } 
    public override string Subtitle { get { return _subtitle; } } 
    public CLLocationCoordinate2D Coords; 
    public override CLLocationCoordinate2D Coordinate { get { return this.Coords; } } 
} 

然後你就可以在你的GetViewForAnnotation()方法使用此批註。 欲瞭解更多信息看這link

+0

基本上我只想在用戶點擊它時顯示當前所選註釋的信息。 – user3020229

+0

嘿,我明白了!發現,我只需要在地圖視圖上調用DidSelectAnnotationView。然後加入我自己的事件,我叫其返回當前選擇的註釋,然後我指責信息來源與自定義類map.selectedannotations你對我的感謝! – user3020229

+0

map.DidSelectAnnotationView + =(對象發件人,MKAnnotationViewEventArgs E)=> { \t \t \t \t annoList = map.SelectedAnnotations; \t \t \t \t的foreach(在annoList IMKAnnotation一個) \t \t \t \t { \t \t \t \t \t SculptureTitle.Text = a.GetTitle(); \t \t \t \t} \t \t \t};; – user3020229

0
map.DidSelectAnnotationView += (object sender, MKAnnotationViewEventArgs e) => 

annoList = map.SelectedAnnotations; 的foreach(在annoList IMKAnnotation一個) { SculptureTitle.Text = a.GetTitle(); } };