使用Apple Maps将选择导航转到注释

时间:2016-09-13 13:51:19

标签: ios swift3 ios10 apple-maps

我的地图上有一个注释显示了一个商业位置和一个按钮,上面写着获取路线,我正在努力获得按钮,为我打开Apple地图,并指示注释位置。这是我到目前为止所做的代码:

import UIKit
import MapKit


class FourthViewController: UIViewController , MKMapViewDelegate {

    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let latitude: CLLocationDegrees = 54.647115
        let longitude: CLLocationDegrees = -6.659070

        let lanDelta: CLLocationDegrees = 0.05

        let lonDelta: CLLocationDegrees = 0.05

        let span = MKCoordinateSpan(latitudeDelta: lanDelta, longitudeDelta: lonDelta)

        let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

        let region = MKCoordinateRegion(center: coordinates, span: span)

        map.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()

        annotation.title = "Pose Beauty Salon"

        annotation.subtitle = "100 Moneyhaw Road"

        annotation.coordinate = coordinates

        map.addAnnotation(annotation)
    }


    @IBAction func mapType(_ sender: AnyObject) {

        switch (sender.selectedSegmentIndex) {
        case 0:
            map.mapType = .standard
        case 1:
            map.mapType = .satellite
        default: // or case 2
            map.mapType = .hybrid
        }

    }

    @IBAction func getDirections(_ sender: AnyObject) {


    }
}

我还看到点击时显示的注释会显示更多信息,例如商家名称,地址,电话号码和网址,这也很难添加?

1 个答案:

答案 0 :(得分:1)

这是我用来解决问题的代码:

let latitude: CLLocationDegrees = 54.647115
        let longitude: CLLocationDegrees = -6.659070
        let url = URL(string: "https://www.posebeautysalon.com")


        let regionDistance:CLLocationDistance = 10000
        let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
        let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
        let options = [
            MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
            MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
        ]
        let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = "Pose Beauty Salon"
        mapItem.phoneNumber = "+442886737777"
        mapItem.url = url
        mapItem.openInMaps(launchOptions: options)