2015-05-07 26 views
0

我有一系列的緯度和經度以及其他東西,我想在Map視圖中創建註釋。我正在使用MapKit。註解在地圖視圖中的標註操作

當用戶單擊註釋標註時,我將顯示另一個控制器。我無法找到用於創建特定註釋的數組元素的索引。

以下是我的代碼。

我有一個int變量

var annotationIndex = 0 

這是我添加註釋

func addAnnotation(latitude: Double, longitude: Double, title: String, subTitle: String?) { 
    if (latitude >= -90 && latitude <= 90) && (longitude >= -180 && longitude <= 180) { 
     let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 
     let span = MKCoordinateSpanMake(0.05, 0.05) 
     let region = MKCoordinateRegion(center: location, span: span) 
     mapView.setRegion(region, animated: true) 

     let annotation = MKPointAnnotation() 
     annotation.coordinate = location 
     annotation.title = title 
     annotation.subtitle = subTitle 
     mapView.addAnnotation(annotation) 
    } 
} 

這是一個MKMapViewDelegate方法來自定義註釋

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! 
{ 
    if !(annotation is MKPointAnnotation) 
    { 
     return nil 
    } 

    let reuseId = "test" 
    var aView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
    aView.canShowCallout = true 

    let btn = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton 
    btn.addTarget(self, action: "btnDisclosureAction:", forControlEvents: UIControlEvents.TouchUpInside) 
    btn.tag = annotationIndex 
    annotationIndex++ 

    aView.rightCalloutAccessoryView = btn 

    return aView 
} 

這是rightCalloutAccessoryView按鈕操作

func btnDisclosureAction(sender: UIButton) { 
    println(sender.tag) 
} 

現在我只是打印標註摺疊式按鈕標籤,我嘗試使用annotationIndex變量來設置它。

由於annotationView被重用,我無法得到確切的索引值。是否有更好的方法來做到這一點?

+0

以下是我的代碼。但是哪裏 ? –

+0

對不起,現在加入 – Matt

+0

如果你能將它轉換成swift,我可以幫助你使用Objective-C。 –

回答

3

具有索引值的子類MKPointAnnotation。

class 
Annotation : MKPointAnnotation { 
    var index = 0 
} 

並設置創建註釋時的索引。

let annotation = Annotation() 

    : 

annotation.subtitle = subTitle 
annotation.index = annotationIndex++ 

通行證註釋對BTN指數

btn.addTarget(self, action: "btnDisclosureAction:", forControlEvents: UIControlEvents.TouchUpInside) 
btn.tag = (annotation as! Annotation).index 

編輯: 所以你viewForAnnotation將

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! 
{ 
    if !(annotation is Annotation) 
    { 
     return nil 
    } 

    let reuseId = "test" 
    var aView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
    aView.canShowCallout = true 

    let btn = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton 
    btn.addTarget(self, action: "btnDisclosureAction:", forControlEvents: UIControlEvents.TouchUpInside) 
    btn.tag = (annotation as! Annotation).index 

    aView.rightCalloutAccessoryView = btn 

    return aView 
} 
+0

你可以發佈「viewForAnnotation」的代碼嗎? – Matt

+1

使用自定義子類而不是MKPointAnnotation _is_進入正確的方向。但是,button標籤(和addTarget)是不必要的,因爲當點擊附件視圖(不需要自定義按鈕處理程序方法)時,地圖視圖將調用'calloutAccessoryControlTapped'委託方法。在該委託方法中,使用'view.annotation'檢索註釋。如果將其轉換爲自定義註記類,則可以直接從註記中檢索自定義索引屬性。 – Anna

+0

謝謝Satachito。我嘗試了完全相同的事情,但沒有發生。你知道任何顯示如何子類MKPointAnnotation的鏈接?我的事情就是我的謊言。 – Matt

0

指定indexselected爲類變量,並在viewDidLoad法-1 initilise它,像

override func viewDidLoad() { 
    super.viewDidLoad() 
    indexselected = -1 
} 

用戶在點擊註釋時可以獲取註釋索引,並且didSelectAnnotationView方法會自動調用。

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) { 
    var selectedAnnotation : MKPointAnnotation = MKPointAnnotation() 
    selectedAnnotation = view.annotation as! MKPointAnnotation 
    for var i = 0; i<yourlist.count ; i++ { 
     let strtitle : String = yourlist.objectAtIndex(i).title as! String 
     if selectedAnnotation.title == strtitle { 
      indexselected=i 
      break 
     } 
    } 
} 

我們可以通過比較數組中標註和標題的標題來獲得標註索引,並保存數組中的索引。在選擇按鈕方法時,您可以使用該索引。

func btnDisclosureAction(sender: UIButton) { 
    println(indexselected) 
} 

希望這對你有所幫助。

+0

謝謝@病毒,但我認爲Satachito答案解決了我的問題,因爲我可能會得到相同的標題,這肯定會成爲問題。無論如何感謝您的時間。 – Matt

+0

歡迎@Matt,Satachito的回答也是一個很好的方法來實現你想要的。 –