Swift 4,启动地图应用,其路线为

时间:2018-06-10 23:56:59

标签: ios swift mapkit

我已经在网上搜索过很多内容,但解释/示例要么不起作用,要么严重过时。

情况: 我有一个带有一些自定义注释的地图。单击注释时,按钮旁边会出现标题和副标题。当我点击该按钮时,它应该启动地图应用程序,并给我从用户位置到自定义引脚注释的指示。

我的问题是当我点击按钮时,没有任何反应。

这是我的代码:

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.
    }

    }

我认为我非常接近在地图中发布方向。我没有得到当前代码的任何错误。

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

为什么不使用calloutAccessoryControlTapped代替addTarget

e.g。

{
    ...
    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()
    }
}