ios - 向用户打印坐标信息,Swift

标签 ios swift dictionary location coordinate

我正在开发 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/

相关文章:

iphone - 创建 CorePlot 图

ios - 在当前用户位置放置图钉 iphone mkmapview

ios - swift 中的 AES 加密

来自两个不同大小数组的 Ruby 数组?

c++ - 如何将映射从类构造函数传递到类中的另一个函数

ios - 放松转场,音频继续播放

Swift - enumerateChildNodesWithName 名称包含字符串

ios - 理解 SwiftUI 的显式对齐

json - 使用 JSON、Swift 4.1 编码的关键困难

java - 3 维列表或 map