您好我正在尝试制作一个按钮,按下该按钮从NSUserDefaults获取位置并转到地图应用程序,允许用户从他们的位置获取路线。出于某种原因,我的代码中出现错误
“CLLocationCoordinate2D没有名为'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)
}
有没有人可以帮助我?感谢!!!
答案 0 :(得分:1)
let SpotLoca = CLLocationCoordinate2DMake(spotLat, spotLng);
...
let location = SpotLoca
...
location.mapItem().openInMapsWithLaunchOptions(launchOptions)
您正在取消引用CLLocationCoordinate2D,它确实没有成员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)