打印坐标信息给用户,Swift
问题描述:
我正在处理一个地图应用程序,到目前为止我有能力丢弃引脚(引脚称为存储器)。我希望在新场景中显示每个丢弃引脚的引脚坐标信息,以便用户可以看到其引脚丢弃的概览。我会如何去做这件事?打印坐标信息给用户,Swift
我已经设置了主视图控制器并添加了一个模态显示的新场景。到目前为止,我有以下代码管理位置和滴/存储针数据:
@IBOutlet weak var addButton: UIBarButtonItem!
// Add button action
@IBAction func addButton(sender: AnyObject) {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: self.placesMap.userLocation.coordinate.latitude, longitude: self.placesMap.userLocation.coordinate.longitude)
self.placesMap.addAnnotation(annotation)
self.locationManager.startUpdatingLocation()
}
// Location function
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.004, longitudeDelta: 0.004))
self.placesMap?.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
let locationDictionary:[String:Double] = ["latitude":center.latitude,"longitude":center.longitude]
var locationArray = [[String:Double]]()
if NSUserDefaults.standardUserDefaults().objectForKey("locationArray") != nil {
locationArray = NSUserDefaults.standardUserDefaults().objectForKey("locationArray") as! [[String:Double]]
}
locationArray.append(locationDictionary)
NSUserDefaults.standardUserDefaults().setObject(locationArray, forKey: "locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error code: " + error.localizedDescription)
}
答
取决于将在其中进行SEGUE,您可以在locationArray传递给performSegueWithIdentifier
发件人的说法,或使其的属性你查看控制器。无论采用哪种方式,您都可以通过UIViewController的prepareForSegue
方法将已丢弃引脚数组注入新场景。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
guard let identifier = segue.identifier else { return }
switch identifier {
case "newScene":
let controller = segue.destinationViewController as! NewSceneViewController
controller.arrayOfDroppedPins = locationArray
// or, controller.arrayOfDroppedPins = sender as! [[String:Double]]
// if you pass in locationArray as the sender
default:
break
}
}
然后在新的场景视图控制器中定义一个属性arrayOfDroppedPins
。