您好,我正在尝试制作一个按钮,按下该按钮时,会从 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/