【问题标题】:Open Apple Maps App With Directions from my ios App iOS 9 Swift 2.2使用我的 ios App iOS 9 Swift 2.2 中的路线打开 Apple Maps App
【发布时间】:2016-08-16 05:33:39
【问题描述】:

我一直在寻找很多教程\帮助,但似乎没有找到任何,所以我再次来到这里.... 当我在我的应用中按下自定义 leftCalloutAccessory 时,我想打开 Apple 地图以获取路线并从用户当前位置导航到选定的 Pin

我已经设置了按钮,但无法使功能正常工作,所以请'如果有人可以指导我完成或帮助我使用代码,这将是一个救生员!谢谢

这是我的代码:

    import UIKit
    import MapKit
    import CoreLocation

    class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate,UISearchBarDelegate{

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()

        override func viewDidLoad() {
            super.viewDidLoad()


            //==========RegionLocation : =========

            // Init the zoom level
            let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 31.30, longitude: 34.45)
            let span = MKCoordinateSpanMake(125, 125)
            let region = MKCoordinateRegionMake(coordinate, span)
            self.mapView.setRegion(region, animated: true)



            //====================================\\
    }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        //Dispose of resources that can be re created.

            }

        //Mark : Location

        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])



        {
            let location = locations.last

            let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.06, longitudeDelta: 0.06))

            self.mapView.setRegion(region, animated: true)

            self.locationManager.stopUpdatingLocation()
        }

        func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
        {
            print("Errors: " + error.localizedDescription)
        }

        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation is MKUserLocation {
                return nil
            }
            let reuseID = "pin"
            var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
            if(pinView == nil) {
                pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
                pinView!.canShowCallout = true
                pinView!.animatesDrop = true
                pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
                let smallSquare = CGSize(width: 30, height: 30)
                let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
                button.setBackgroundImage(UIImage(named: "Car"), forState: .Normal)
                pinView?.leftCalloutAccessoryView = button
            }
            else
            {
                pinView!.annotation = annotation
            }


            return pinView

        }

        func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {

            if Control == annotationView.leftCalloutAccessoryView {

            }

}

【问题讨论】:

    标签: ios swift xcode uibutton mkannotation


    【解决方案1】:

    以下函数将打开 Apple Maps 应用程序,并显示从用户当前位置到坐标处指定目的地的行车路线。目的地名称显示在地图应用的 To: 字段中。

    func openMapsAppWithDirections(to coordinate: CLLocationCoordinate2D, destinationName name: String) {
      let options = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
      let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
      let mapItem = MKMapItem(placemark: placemark)
      mapItem.name = name // Provide the name of the destination in the To: field
      mapItem.openInMapsWithLaunchOptions(options)
    }
    

    您可以从代码中调用此函数,如下所示:

    func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {
    
      if Control == annotationView.leftCalloutAccessoryView {
        if let annotation = annotationView.annotation {
          // Unwrap the double-optional annotation.title property or
          // name the destination "Unknown" if the annotation has no title
          let destinationName = (annotation.title ?? nil) ?? "Unknown"
          openMapsAppWithDirections(to: annotation.coordinate, destinationName: destinationName)
        }
      }
    
    }
    

    【讨论】:

    • 嘿,首先,在将代码添加到我的项目后,我会立即提示我将 requestDirections 错误修复到requestDirections(from: MapView.userLocation.location!.coordinate, to:annotationView.annotation!.coordinate),如果它很重要的话....其次,在应用程序本身中没有任何反应它不会打开苹果地图,但会在调试器中显示逐步说明
    • 我的回答中的requestDirections 函数演示了如何检索两个坐标之间的方向。您如何呈现这些方向取决于您的应用程序。例如,如果要将方向显示为地图上的一条线,可以使用MKRoute 对象的polyline 属性添加折线注释。我希望这会有所帮助。
    • 我明白了,感谢您的帮助,我还更新了我的问题,以便更准确地了解我在寻找什么。
    • 我已更新我的答案以帮助您解决重命名和改写的问题。
    • 嘿,它工作正常!但是它说它指向“未知位置”....有没有办法将其更改为注释标题或其他内容而不是“未知位置”?thx
    【解决方案2】:

    像这样创建扩展:

    extension CLLocation {
    
        func drive() {
    
            let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDictionary)
    
            let launchOptions = [
                MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
            ]
    
            MKMapItem.openMaps(with: [MKMapItem.forCurrentLocation(), MKMapItem(placemark: placemark)], launchOptions: launchOptions)
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-23
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多