在 Swift 中转到地图应用程序
Segueing to the Maps App in Swift
您好,我正在尝试制作一个按钮,当按下该按钮时,它会从 NSUserDefaults 获取一个位置并转到地图应用程序,从而允许用户从他们的位置获取路线。出于某种原因,我的代码中出现错误
"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)
您正在取消引用 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)
您好,我正在尝试制作一个按钮,当按下该按钮时,它会从 NSUserDefaults 获取一个位置并转到地图应用程序,从而允许用户从他们的位置获取路线。出于某种原因,我的代码中出现错误
"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)
您正在取消引用 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)