我有一系列的纬度和经度以及其他东西,我想在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被重用,我无法得到确切的索引值。是否有更好的方法来做到这一点?
以下是我的代码。但是哪里 ? –
对不起,现在加入 – Matt
如果你能将它转换成swift,我可以帮助你使用Objective-C。 –