我有一系列纬度和经度以及其他东西,我想在地图视图中创建注释。我正在使用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)
}
现在我只是打印callout diusclosure按钮标签,我尝试使用annotationIndex变量进行设置。
由于annotationView被重用,我无法获得确切的索引值。有更好的方法吗?
答案 0 :(得分: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
}
答案 1 :(得分:0)
将$(document).ready(function()
{
$('#SubmitForm').on('submit', function(e)
{
e.preventDefault();
$('#submitButton').attr('disabled', '');
$(this).ajaxSubmit({
target: '#output',
success: afterSuccess //call function after success
});
});
});
function afterSuccess()
{
$('#submitButton').removeAttr('disabled'); //enable submit button
}
指定为类变量,并在indexselected
方法中使用-1初始化它,例如
viewDidLoad
您可以在用户点按注释时获取注释索引,并自动调用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
}
}
}
希望这对你有所帮助。