【问题标题】:iOS - Open Apple Maps with latitude and longitude directionsiOS - 打开带有纬度和经度方向的 Apple 地图
【发布时间】:2016-11-16 10:07:00
【问题描述】:

我正在开发一个应用程序,需要打开 Apple Maps 会话,并传入纬度和经度坐标以获取从用户当前位置到该位置的路线。

我知道这可以在我已经在做的谷歌地图中完成,但是当尝试在 Apple Maps 中打开 URL 时,它只会打开该位置,而不是从用户当前位置到目的地的路线。

这是我一直使用的 URL 方案:

http://maps.apple.com/?ll=(someLatitude),(someLongitute)

代码:

UIApplication.sharedApplication().openURL(NSURL(string:"http://maps.apple.com/?ll=\(locationLat),\(locationlong)")!)

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 您应该使用您的位置创建一个MKMapItem,然后使用openInMapsWithLaunchOptions() 方法打开苹果地图
  • 嗯,有道理。谢谢!
  • 试试这个代码:link
  • 谢谢,Matthijs,但我已经可以用我拥有的代码做到这一点,只是无法打开方向。
  • 查看this question的答案,了解如何使用openInMapsWithLaunchOptions

标签: ios swift apple-maps


【解决方案1】:

试试这个代码,AppleMap 将打开从设备当前位置标记到指定坐标位置的方向。

        let coordinate = CLLocationCoordinate2DMake(currentLat, currentLong)
        let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
        mapItem.name = “Destination/Target Address or Name”
        mapItem.openInMapsWithLaunchOptions([MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])

【讨论】:

    【解决方案2】:

    尝试使用

    NSURL(string:"http://maps.apple.com/?saddr=\(currentLat),\(currentLong)&daddr=\(destinationLat),\(destinationLong)")!
    

    其中currentLat currentLong是用户当前位置,destinationLat destinationLong是目标位置。

    更多参数(例如:传输类型)查看here

    【讨论】:

      【解决方案3】:

      基于larva's answer,但我建议您使用"maps://" URL 方案而不是"http://maps.apple.com/" 绕过打开Safari。

      喜欢这里:

      URL(string: "maps://?saddr=\(currentLat),\(currentLong)&daddr=\(destinationLat),\(destinationLong)")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        • 1970-01-01
        • 2020-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-03
        相关资源
        最近更新 更多