首先请不要将此标记为重复。我已经完成了所有其他问题和答案,并且已经改进了我第一次使用的代码,但我仍然无法弄清楚最后一部分。我明天将有我的最后一年项目,并希望这个功能正常运行。谢谢!!
我有一个来自注释的自定义标注,当我点击它上面的按钮时,我想获得从用户位置到该注释的指示。
let buttonDirections = UIButton(frame: calloutView.directions.frame)
buttonDirections.addTarget(self, action: #selector(FindParkingVC.getDirections(sender:)), for: .touchUpInside)
calloutView.addSubview(buttonDirections)
func getDirections(sender: UIButton) {
//error on this line saying UIbutton has no member view
//obviously the sender is wrong but don't know how to fix it
if let anno = sender.view.annotation as? SpaceAnnotation
{
var place: MKPlacemark!
if #available(iOS 10.0, *) {
place = MKPlacemark(coordinate: anno.coordinate)
} else {
place = MKPlacemark(coordinate: anno.coordinate, addressDictionary: nil)
}
let destination = MKMapItem(placemark: place)
destination.name = "Selected Parking Space"
let regionDistance: CLLocationDistance = 1000
let regionSpan = MKCoordinateRegionMakeWithDistance(anno.coordinate, regionDistance, regionDistance)
let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span), MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] as [String : Any]
MKMapItem.openMaps(with: [destination], launchOptions: options)
}
}
现在,当我使用标准标注时,我之前已经完成了所有工作。这个功能完全像这样。我现在没有使用detailcalloutbutton所以我不能使用这个功能。
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if let anno = view.annotation as? SpaceAnnotation {
var place: MKPlacemark!
if #available(iOS 10.0, *) {
place = MKPlacemark(coordinate: anno.coordinate)
} else {
place = MKPlacemark(coordinate: anno.coordinate, addressDictionary: nil)
}
let destination = MKMapItem(placemark: place)
destination.name = "Selected Parking Space"
let regionDistance: CLLocationDistance = 1000
let regionSpan = MKCoordinateRegionMakeWithDistance(anno.coordinate, regionDistance, regionDistance)
let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span), MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] as [String : Any]
MKMapItem.openMaps(with: [destination], launchOptions: options)
}
}
修改
这就是我调用CustomCallout View的方式
func mapView(_ mapView: MKMapView,
didSelect view: MKAnnotationView)
{
if view.annotation is MKUserLocation
{
// Don't proceed with custom callout
return
}
let SpaceAnnotation = view.annotation as! SpaceAnnotation
let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
let calloutView = views?[0] as! CustomCalloutView
if SpaceAnnotation.temp3 != nil {
calloutView.duration.text = "Available for " + SpaceAnnotation.temp3
}
if SpaceAnnotation.temp != nil {
calloutView.price.text = "€" + SpaceAnnotation.temp
}
if SpaceAnnotation.temp2 != nil {
calloutView.description_.text = "Description: " + SpaceAnnotation.temp2
}
let buttonDirections = UIButton(frame: calloutView.directions.frame)
buttonDirections.addTarget(self, action: #selector(FindParkingVC.getDirections(sender:)), for: .touchUpInside)
calloutView.addSubview(buttonDirections)
calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
view.addSubview(calloutView)
mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}
也许值得注意的是SpaceAnnotation是一个内容变量temp3等的类,所以在init中它会有更多以下
temp3 = snapshotValue["duration"] as? String
答案 0 :(得分:0)
UIButton
已添加到CustomCalloutView
,然后已添加到MKAnnotationView
。因此,MKAnnotationView
是超级视图的超级视图(两步):
if let anno = (sender.superview?.superview as! MKAnnotationView).annotation as? SpaceAnnotation {
}