首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Swift 4,启动带方向的地图应用程序

Swift 4,启动带方向的地图应用程序
EN

Stack Overflow用户
提问于 2018-06-11 07:56:59
回答 1查看 102关注 0票数 -1

我已经在网上搜索了很多次,但是解释/例子要么不起作用,要么已经非常过时了。

情景:我有一个带有一些自定义注释的地图。当我点击一个注解时,一个标题和一个副标题就会出现在按钮旁边。当我点击那个按钮时,它应该会启动地图应用程序,并为我提供从用户位置到自定义引脚注释的方向。

我的问题是,当我点击按钮时,什么也没有发生。

下面是我的代码:

代码语言:javascript
复制
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.
    }

    }

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

感谢您的帮助!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50789056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档