我正在开发 map 应用程序,到目前为止我有能力放置图钉(图钉称为存储器)。我希望在新场景中显示每个掉落的引脚的引脚坐标信息,以便用户可以看到其掉落的引脚的概述。我该如何去做呢?
我已经设置了主视图 Controller 并添加了一个以模态方式显示的新场景。到目前为止,我有以下代码来管理位置并删除/存储 pin 数据:
@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
发送者的参数,或者将其作为 View Controller 的属性。无论哪种方式,您都可以通过 UIViewController 的 prepareForSegue
方法将放置的引脚数组注入(inject)到新场景中。
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
}
}
然后在新的场景 View Controller 中定义一个属性arrayOfDroppedPins
。
关于ios - 向用户打印坐标信息,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36825759/