地图视图中注释的标注操作
Callout action from Annotations in Map view
我有一组纬度和经度以及其他内容,我想在地图视图中创建注释。我正在使用 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
}
这是正确的 CalloutAccessoryView 按钮操作
func btnDisclosureAction(sender: UIButton) {
println(sender.tag)
}
现在我只是打印 callout diusclosure 按钮标签,我尝试使用 annotationIndex 变量设置它。
由于 annotationView 被重用,我无法获得准确的索引 value.Is 有更好的方法吗?
子类 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
}
将indexselected
指定为class变量并在viewDidLoad
方法中用-1初始化它,比如
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)
}
希望对您有所帮助。
我有一组纬度和经度以及其他内容,我想在地图视图中创建注释。我正在使用 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
}
这是正确的 CalloutAccessoryView 按钮操作
func btnDisclosureAction(sender: UIButton) {
println(sender.tag)
}
现在我只是打印 callout diusclosure 按钮标签,我尝试使用 annotationIndex 变量设置它。
由于 annotationView 被重用,我无法获得准确的索引 value.Is 有更好的方法吗?
子类 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
}
将indexselected
指定为class变量并在viewDidLoad
方法中用-1初始化它,比如
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)
}
希望对您有所帮助。