我已经在网上搜索了很多次,但是解释/例子要么不起作用,要么已经非常过时了。
情景:我有一个带有一些自定义注释的地图。当我点击一个注解时,一个标题和一个副标题就会出现在按钮旁边。当我点击那个按钮时,它应该会启动地图应用程序,并为我提供从用户位置到自定义引脚注释的方向。
我的问题是,当我点击按钮时,什么也没有发生。
下面是我的代码:
import UIKit
import MapKit
import CoreLocation
class customPin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
self.title = pinTitle
self.subtitle = pinSubTitle
self.coordinate = location
}
}
class ViewController: UIViewController, MKMapViewDelegate {
var selectedPin:MKPlacemark? = nil
let locationManager = CLLocationManager()
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
NotificationCenter.default.addObserver(self,
selector: #selector(self.applicationDidResignActive),
name: Notification.Name.UIApplicationWillResignActive,
object: nil)
//mapView.setRegion(MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(62.524459, 6.630952 ), 955000, 955000), animated: true)
// add annotations
//PART 1
let location = CLLocationCoordinate2DMake(62.524459, 6.630952)
let location2 = CLLocationCoordinate2DMake(62.622460, 7.009174 )
let location3 = CLLocationCoordinate2DMake(62.491195, 6.606216 )
//PART 2
let pin = customPin(pinTitle: "Bunker Oil Stette, 6265",
pinSubTitle:"Bensin",
location:location)
let pin2 = customPin(pinTitle: "Bensin" ,
pinSubTitle:"Shit",
location:location2)
let pin3 = customPin(pinTitle: "Shit" , pinSubTitle:"Adres35", location:location3)
//PART 3
mapView.addAnnotation(pin)
mapView.addAnnotation(pin2)
mapView.addAnnotation(pin3)
}
@objc func applicationDidResignActive(notification: NSNotification) {
// handle event
}
@objc func getDirections(){
guard let selectedPin = selectedPin else { return }
let mapItem = MKMapItem(placemark: selectedPin)
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
mapItem.openInMaps(launchOptions: launchOptions)
print ("GET DIRECTION")
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
if annotation.subtitle == "Bensin" {
annotationView.image = UIImage(named:"rBluepin")
} else if annotation.subtitle == "Shit" {
annotationView.image = UIImage(named:"rPurple")
} else {
annotationView.image = UIImage(named:"darkgreenpin")
}
// ------ UI BUTTON -----
let image1 = UIImage(named: "rRedpin")!
let rightButton = UIButton(type: .contactAdd)
rightButton.tag = annotation.hash
rightButton.setImage(image1, for: UIControlState.normal)
//annotationView.animatesDrop = true
annotationView.canShowCallout = true
annotationView.rightCalloutAccessoryView = rightButton
rightButton.addTarget(self, action: #selector(ViewController.getDirections), for: .touchUpInside)
annotationView.leftCalloutAccessoryView = rightButton
annotationView.canShowCallout = true
return annotationView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("annotation title == \(String(describing: view.annotation?.title!))")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我想我已经非常接近在地图上发布方向了。我在当前代码中没有得到任何错误。
感谢您的帮助!
发布于 2018-06-11 08:28:32
你为什么不用addTarget
代替calloutAccessoryControlTapped
呢?
例如:
{
...
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.canShowCallout = true
let rightButton = UIButton(type: UIButtonType.detailDisclosure)
pinView?.rightCalloutAccessoryView = rightButton
...
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print(#function)
if control == view.rightCalloutAccessoryView {
getDirection()
}
}
https://stackoverflow.com/questions/50789056
复制相似问题