【问题标题】:Printing coordinate information to user, Swift向用户打印坐标信息,Swift
【发布时间】:2016-04-24 16:20:17
【问题描述】:

我正在开发一个地图应用程序,到目前为止,我能够放置图钉(图钉称为记忆)。我希望在新场景中显示每个掉落的针脚的坐标信息,以便用户可以看到他们掉落的针脚的概览。我该怎么做呢?

我已经设置了主视图控制器并添加了一个模态显示的新场景。到目前为止,我有以下代码来管理位置和删除/存储 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)
}

【问题讨论】:

    标签: ios swift dictionary location coordinate


    【解决方案1】:

    取决于执行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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      相关资源
      最近更新 更多