Ios 如何使用swift中的坐标以编程方式打开地图应用程序?

Ios 如何使用swift中的坐标以编程方式打开地图应用程序?,ios,swift,apple-maps,Ios,Swift,Apple Maps,我想在地图应用程序中打开纬度和经度。我试过这个代码 此函数成功打开地图,但不显示任何pin。它还显示了我不想要的用户位置。我只需要地图上提供的纬度和经度的pin。此代码对我来说运行良好 func openMapForPlace() { let lat1 : NSString = self.venueLat let lng1 : NSString = self.venueLng let latitude:CLLocationDegrees = lat1

我想在地图应用程序中打开纬度和经度。我试过这个代码


此函数成功打开地图,但不显示任何pin。它还显示了我不想要的用户位置。我只需要地图上提供的纬度和经度的pin。

此代码对我来说运行良好

func openMapForPlace() {
    
    let lat1 : NSString = self.venueLat
    let lng1 : NSString = self.venueLng
    
    let latitude:CLLocationDegrees =  lat1.doubleValue
    let longitude:CLLocationDegrees =  lng1.doubleValue
    
    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 = "\(self.venueName)"
    mapItem.openInMapsWithLaunchOptions(options)
    
}
对于swift 3.0:

import UIKit
import MapKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        openMapForPlace()
    }
    
    func openMapForPlace() {
        
        let latitude: CLLocationDegrees = 37.2
        let longitude: CLLocationDegrees = 22.9
        
        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 = "Place Name"
        mapItem.openInMaps(launchOptions: options)
    }
}
Swift 5:

let latitude: CLLocationDegrees = Double(K.latitude)!
let longitude: CLLocationDegrees = Double(K.longitude)!
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: 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 = K.companyName
mapItem.openInMaps(launchOptions: options)

如果您只想为用户提供驾驶方向,以下是最新的最简单形式的Swift语法:

let coordinate = CLLocationCoordinate2DMake(theLatitude,theLongitude)
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
mapItem.name = "Target location"
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])

这对我很有吸引力

let coordinate = CLLocationCoordinate2DMake(theLatitude, theLongitude)
let region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.01, 0.02))
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
let options = [
    MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center),
    MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)]
mapItem.name = theLocationName
mapItem.openInMaps(launchOptions: options)

如果您想要精确控制地图中显示的信息,上面的
MKMapItem
方法非常有效

否则,下面的代码也很有用:

// Open and show coordinate
let url = "http://maps.apple.com/maps?saddr=\(coord.latitude),\(coord.longitude)"
UIApplication.shared.openURL(URL(string:url)!)

// Navigate from one coordinate to another
let url = "http://maps.apple.com/maps?saddr=\(from.latitude),\(from.longitude)&daddr=\(to.latitude),\(to.longitude)"
UIApplication.shared.openURL(URL(string:url)!)
但是,上面的代码不允许您发送该地点的自定义名称。相反,它将显示地址


上面的代码还允许您从任何源坐标进行导航,我不知道您是否可以使用MKMapItem方法进行导航。

您可以调用
MKMapItem
的类函数在那里传递项目,如果您想要传递两个以上的项目,它只适当地使用源/目标的first和last

斯威夫特5号,4号 或使用扩展名:

extension MKMapItem {
  convenience init(coordinate: CLLocationCoordinate2D, name: String) {
    self.init(placemark: .init(coordinate: coordinate))
    self.name = name
  }
}

let source = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Source")
let destination = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Destination")

MKMapItem.openMaps(
  with: [source, destination], 
  launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
)

您可以使用下面的代码在lat上显示PIN,长在苹果地图上

let coordinates = CLLocationCoordinate2DMake(-37.848854,144.990295)

let regionSpan =   MKCoordinateRegionMakeWithDistance(coordinates, 1000, 1000)

let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)

let mapItem = MKMapItem(placemark: placemark)

mapItem.name = “Desired place”

mapItem.openInMaps(launchOptions:[
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center)
] as [String : Any])

如果您想要的是一些简单的东西,而不需要导入任何框架,那么您可以创建一个URL:
https://maps.apple.com/?ll=\(纬度),\(经度)


与@saniel saidi响应类似,但这一响应仅打开发送位置的地图,而不是导航内容

我知道所有答案都已完成,但我在这里得到了一个更易于复制粘贴的答案,并为用户提供了使用Apple Maps、Google map和Waze进行路由的选项

使用Swift 5+


可能会帮助某人…

更新实用的Daniel Saidi。此示例仅用于告知目标坐标。地图将获取用户当前位置作为原点

let url = URL(string: "http://maps.apple.com/maps?saddr=&daddr=\(lat),\(lon)")
UIApplication.shared.open(url!)


打开Apple Maps应用程序并在自定义位置显示带有自定义标题的pin的最简单方法是:

let url = URL(string: "maps://?q=Title&ll=\(latitude),\(longitude)")!
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url)
}
在此处查看更多选项:

此代码旨在显示从用户位置到目标位置的行驶方向。要仅显示单个目标,请使用mklaunchoptions映射中心键和单个映射项。请参阅。感谢您的建议。Anna。您能否确认mklaunchoptions映射span键选项具有所需的效果?无论我使用CLLocationDistance的值是多少,地图都被放大了。当一个或多个
MKMapItem
添加到地图中时,
mklaunchoptions映射span键似乎被忽略了:别忘了:import mapkit只是想指出它是“纬度”和“经度”,而不是“纬度”和“经度”“tute”很有趣如果用户删除了地图应用程序,我们如何检查该案例???这对我来说效果很好,只是启动选项中的键和值是相反的。应该是
mapItem.openinmpswithlaunchoptions([mklaunchoptions方向modekey:mklaunchoptions方向modedriving])
确保将导入地图工具包添加到文件顶部以解决错误。在swift 3中,最后一行现在是…mapItem.openInMaps(launchOptions:[mklaunchOptions Directions ModeKey:mklaunchOptions Directions ModeDrive]),如果您将“saddr=”保留为空,应用程序会将“您的位置”设置为默认值。类似于http://maps.apple.com/maps?saddr=&daddr=(to.latitude),(to.longitude)“方向不可用”这已不再适用于我。我的结果是“没有可用的方向”。只需删除()在坐标附近,没有找到它。嗨,dimpiax!谢谢你的回答,没有测试它,但看起来很好。有可能发送几个航路点吗?如何!你能帮我吗。@Frade嘿,有了MapKit你不能,看到了:但是你可以用谷歌地图来实现。是的,几分钟后我看到了。谢谢
let url = URL(string: "http://maps.apple.com/maps?saddr=&daddr=\(lat),\(lon)")
UIApplication.shared.open(url!)

let url = URL(string: "maps://?q=Title&ll=\(latitude),\(longitude)")!
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url)
}