swift - 在 Swift 中跳转到 map 应用程序

标签 swift mapkit nsuserdefaults

您好,我正在尝试制作一个按钮,按下该按钮时,会从 NSUserDefaults 获取位置并转到 map 应用程序,从而允许用户从其位置获取方向。由于某种原因,我的代码中出现错误,内容为

"CLLocationCoordinate2D does not have a member named 'mapItem'"

这是我的代码

@IBAction func DirectionsButton(sender: AnyObject) {

    let spotTitle = NSUserDefaults.standardUserDefaults().objectForKey("SpotTitle") as! String
    let spotLoc = NSUserDefaults.standardUserDefaults().objectForKey("SpotLoc") as! [String : NSNumber]
    //Get user location from that Dictionary
    let spotLat = spotLoc["lat"] as! CLLocationDegrees //Convert NSNumber to CLLocationDegrees
    let spotLng = spotLoc["lng"] as! CLLocationDegrees  //Convert NSNumber to CLLocationDegrees


    let SpotLoca = CLLocationCoordinate2DMake(spotLat, spotLng);
    func mapItem() -> MKMapItem {
        let addressDictionary = [String(kABPersonAddressStreetKey): spotTitle]
        let placemark = MKPlacemark(coordinate: SpotLoca, addressDictionary: addressDictionary)

        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = title

        return mapItem
    }
    let location = SpotLoca
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
    location.mapItem().openInMapsWithLaunchOptions(launchOptions)
}

有谁可以帮助我吗?谢谢!!!

最佳答案

let SpotLoca = CLLocationCoordinate2DMake(spotLat, spotLng);
...
let location = SpotLoca
...
location.mapItem().openInMapsWithLaunchOptions(launchOptions)

您正在取消引用 CLLocationCooperative2D,它确实没有成员 mapItem。我认为你的最后一行应该是:

mapItem().openInMapsWithLaunchOptions(launchOptions)

或者,您可以删除该函数并像这样执行:

let spotTitle = NSUserDefaults.standardUserDefaults().objectForKey("SpotTitle") as! String
let spotLoc = NSUserDefaults.standardUserDefaults().objectForKey("SpotLoc") as! [String : NSNumber]
//Get user location from that Dictionary
let spotLat = spotLoc["lat"] as! CLLocationDegrees //Convert NSNumber to CLLocationDegrees
let spotLng = spotLoc["lng"] as! CLLocationDegrees  //Convert NSNumber to CLLocationDegrees


let SpotLoca = CLLocationCoordinate2DMake(spotLat, spotLng);
let addressDictionary = [String(kABPersonAddressStreetKey): spotTitle]
let placemark = MKPlacemark(coordinate: SpotLoca, addressDictionary: addressDictionary)

let mapItem = MKMapItem(placemark: placemark)
mapItem.name = title

let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
mapItem.openInMapsWithLaunchOptions(launchOptions)

关于swift - 在 Swift 中跳转到 map 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909975/

相关文章:

ios - Swift:Map 返回 4 个值作为 2 点之间的距离,为什么?

ios - 使用 Swift 4.1 编译的模块无法在 Swift 3.2.3 中导入

ios - 从特定年份的给定月份获取每一天

ios - 地标和注释之间的区别

ios - 二元运算符 '+' 不能应用于类型 'String' 和“字符串感叹号”的操作数

swift - 如何快速向服务器发出 HTTPS 请求?

ios - 使用 swift 和 market 查找附近的酒吧

iphone - 如何更改自定义 MKAnnotationView 的框架位置?

iphone - iOS 4 上的设置不会更新

ios - 如何使用 NSUserDefaults 将 UIImages 保存在应用程序目录中并在 UITableViewCell 中显示它们?