如何启动地图应用并开始导航

Sag*_*usA 2 google-maps mapkit ios swift

在我的iOS应用程序中,我有CLLocationCoordinate2D一个用户想要到达的地方的纬度和经度().我想要的是,当按下相对按钮时,将启动地图应用程序并启动到该地点的街道导航.我怎样才能做到这一点?我的代码到目前为止:

@IBAction func launchMapsApp(sender:UIButton) {
    if (sender == self.navButton) {

        let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: self.currentCoordinates, addressDictionary: nil))

        mapItem.name = ""

        //You could also choose: MKLaunchOptionsDirectionsModeWalking
        let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: true]

        mapItem.openInMapsWithLaunchOptions(launchOptions as? [String : AnyObject])
    }

}
Run Code Online (Sandbox Code Playgroud)

但有了这个,地图应用程序只是启动,我只是看到我的州(意大利)的地图,没有更多的事情发生.也许,因为我只在模拟器中运行?谢谢大家

Sam*_*elD 6

你走在正确的轨道上:这是我的swift 2.0代码:

let latitude:CLLocationDegrees = xx.xxxxx
let longitude: CLLocationDegrees = xx.xxxxx
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 = "Name you want"
mapitem.openInMapsWithLaunchOptions(options)
Run Code Online (Sandbox Code Playgroud)

我希望我的代码能给你一些见解.

  • 这是您在地图上看到的距离。那么如果是10000(米)=10公里。您将看到该点的左侧、右侧、顶部和底部各 5 公里(纬度、经度)。 (2认同)

Mik*_*her 5

您的原始代码看起来是正确的。正如您所说,问题可能在于您如何使用模拟器。您可以通过模拟器中的调试菜单(位置 -> 自定义)设置模拟位置。

要启动地图并开始导航到特定位置(而不是像在另一个答案中那样仅在所需位置打开带有标记的地图),我使用了这个(在 Swift 3 中):

let coordinates = CLLocationCoordinate2DMake(destLatitude, destLongitude)
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapitem = MKMapItem(placemark: placemark)
let options = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
mapitem.openInMaps(launchOptions: options)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助...