【问题标题】:get direction and route with MKMapView使用 MKMapView 获取方向和路线
【发布时间】:2017-11-09 23:48:15
【问题描述】:

我一直试图在不使用地图或谷歌地图应用程序的情况下使用 MKMapView 获取方向和路线。 我想在我的应用程序中使用它并打开其他应用程序。 有人可以帮忙吗?

这是我所拥有的,但我想更改它,以便我可以在我的应用程序上获得方向和路线。我有 Swift 4 和 Xcode 9!

//Getting direction of location
    @objc func getDirections(){
        if let selectedPin = selectedPin {
            let mapItem = MKMapItem(placemark: selectedPin)
            let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
            mapItem.openInMaps(launchOptions: launchOptions)
        }
    }


extension ViewController : MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }
        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.pinTintColor = UIColor.red
        pinView?.canShowCallout = true
        let smallSquare = CGSize(width: 30, height: 30)
        let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
        button.setBackgroundImage(UIImage(named: "Car"), for: .normal)
        button.addTarget(self, action: #selector(getDirections), for: .touchUpInside)

        pinView?.leftCalloutAccessoryView = button
        return pinView
    }
}

【问题讨论】:

    标签: swift mapkit


    【解决方案1】:

    我一直试图在不使用地图或谷歌地图应用程序的情况下使用 MKMapView 获取方向和路线。 我想在我的应用程序中使用它并打开其他应用程序。有人可以帮忙吗?

    您无法在应用内启动导航。您可以做的是在两点之间绘制一条路径并获取距离或预期的驾驶/步行时间等信息。

    搜索Distance between two points MapKit,你会找到一堆结果。例如看看hereherethis swift 4 soultion

    【讨论】:

      猜你喜欢
      • 2010-12-02
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多