首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打开苹果地图应用程序,提供我的ios应用程序iOS 9 SWIFT2.2的说明

打开苹果地图应用程序,提供我的ios应用程序iOS 9 SWIFT2.2的说明
EN

Stack Overflow用户
提问于 2016-08-16 05:33:39
回答 2查看 5.9K关注 0票数 4

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

我已经设置了按钮,但坎特得到的功能工作,所以请‘如果有人可以指导我通过或帮助我的代码,将是一个生命拯救!thx

这是我的密码:

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

            }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-18 07:52:09

下面的功能将打开Apple应用程序,并在坐标下显示从用户当前位置到指定目的地的驾驶方向。目标名称显示在Maps应用程序的To:字段中。

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

您可以按如下方式从代码中调用此函数:

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

}
票数 4
EN

Stack Overflow用户

发布于 2017-05-10 12:00:29

创建如下扩展:

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

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

https://stackoverflow.com/questions/38967259

复制
相关文章

相似问题

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