问题描述
我在地图内做了一些注释,当我点击它们时,我会看到一些信息,我有一个按钮可以打开地图,如果我无法获取正确的信息,它应该可以绘制我的路线.
I've made a few annotations inside the Map and when I tap on them I see some informations and I have a button that opens Maps and with the right information that I can't take it should draw me my route.
这是我的代码:
我的 lat & 有 2 个双精度数组lon 我从查询中填写了它们.
I have 2 arrays of double for my lat & lon That I filled them from my query.
var lat = [Double]()
var lon = [Double]()
这些行用于填充注释
self.annot = Islands(title: object["name"] as! String, subtitle: object["address"] as! String, coordinate: CLLocationCoordinate2D(latitude: (position?.latitude)!, longitude: (position?.longitude)!), info: object["address"] as! String)
self.map.addAnnotations([self.annot])
这是创建注释的地方:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "pin"
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let image = UIImage(named: "car")
let button = UIButton(type: .Custom)
button.frame = CGRectMake(0, 0, 30, 30)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: #selector(Map.info(_:)), forControlEvents:UIControlEvents.TouchUpInside)
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView!.canShowCallout = true
annotationView!.image = UIImage(named: "annotation")
annotationView!.rightCalloutAccessoryView = button
}
else {
annotationView!.annotation = annotation
}
return annotationView
}
最后这是按钮的功能,我应该在其中传递正确的信息(以及问题所在)
And finally this is the button's function where I should pass the right info ( and where the problem is)
func info(sender: UIButton)
{
let latitute:CLLocationDegrees = lat[sender.tag]
let longitute:CLLocationDegrees = lon[sender.tag]
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitute, longitute)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = name[sender.tag]
print(sender.tag)
mapItem.openInMapsWithLaunchOptions(options)
}
如您所见,我尝试使用发件人标签,但没有成功.
As you can see I tried with sender tag but with no luck.
我的自定义注释类
class Islands: NSObject, MKAnnotation {
var title: String?
var addr: String?
var coordinate: CLLocationCoordinate2D
var info: String?
var subtitle: String?
init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D, info: String) {
self.title = title
self.coordinate = coordinate
self.info = info
self.subtitle = subtitle
}
}
推荐答案
为此,您可以使用 didSelectAnnotationView
中选定的 Annotation
,然后将该注释存储到实例变量中,然后在您的 Button
操作方法中使用了注解.
For that you can use selected Annotation
from didSelectAnnotationView
, then store that annotation to instance variable and after that used annotation in your Button
action method.
var selectedAnnotation: MKPointAnnotation?
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
self.selectedAnnotation = view.annotation as? MKPointAnnotation
}
func info(sender: UIButton) {
print(selectedAnnotation?.coordinate)
}
因为你有自定义 MKAnnotation
类,你需要使用它.
As of you have custom MKAnnotation
class you need to use that.
var selectedAnnotation: Islands?
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
self.selectedAnnotation = view.annotation as? Islands
}
这篇关于如何检测在 MapView 中选择了哪个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!