2015-05-07 30 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的回答也是一个很好的方法来实现你想要的。 –