SWIFT 4,发布带有方向的地图应用程序

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (14)

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

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

我的问题是,当我单击按钮时,我的应用程序崩溃或没有任何反应。

这是我的代码:

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 getDirections(){
        if let selectedPin = selectedPin {
            let mapItem = MKMapItem(placemark: selectedPin)
            let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
            mapItem.openInMaps(launchOptions: launchOptions)
        } */


    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")!
        // in case you don't want image to change when "clicked", you can leave code below


        let rightButton = UIButton(type: .contactAdd)
        rightButton.tag = annotation.hash
        rightButton.setImage(image1, for: UIControlState.normal)



        //annotationView.animatesDrop = true
        annotationView.canShowCallout = true
        annotationView.rightCalloutAccessoryView = rightButton


        // ---- GET DIRECTIONS

        //let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
        //let mapItem = MKMapItem(placemark: annotation as! MKPlacemark)
        //let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
        //mapItem.openInMaps(launchOptions: launchOptions)


        //rightButton.setBackgroundImage(UIImage(named: "rRedpin"), for: .normal)
        //rightButton.addTarget(self, action: Selector(("getDirections")), for: .touchUpInside)
        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.
    }

    }

    extension ViewController : CLLocationManagerDelegate {

    private func locationManager(  manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            locationManager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else { return }
        let span = MKCoordinateSpanMake(1.120, 1.120)
        let region = MKCoordinateRegion(center: location.coordinate, span: span)
        mapView.setRegion(region, animated: true)
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("error:: \(error)")
    }
}

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

模拟器的屏幕截图 - > 模拟器的屏幕截图

感谢帮助!

提问于
用户回答回答于

您的getDirections代码依赖于实例属性selectedPin,声明如下:

var selectedPin:MKPlacemark? = nil

你所展示的代码都没有设置过 selectedPin,所以没有任何代码可以改变它nil。因此,当你说(在开始时getDirections):

guard let selectedPin = selectedPin else { return }

......我期待着我们return,没有任何事情发生。

(我已经解释过“没有任何反应”。我不知道为什么你的应用程序会崩溃,但是你在问题上有责任提供崩溃日志或其他有关崩溃的信息,所以我不负责跟踪也许崩溃来自你添加guard let声明之前的时间?)

用户回答回答于

你为什么不用calloutAccessoryControlTapped而不是addTarget

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

扫码关注云+社区

领取腾讯云代金券