【问题标题】:Adding timestamp to MapView Annotation via Parse?通过 Parse 将时间戳添加到 MapView 注释?
【发布时间】:2015-04-25 02:29:22
【问题描述】:

这是我当前的查询设置,用于从解析中检索数据并将其设置到 mapView 上的注释引脚。我也想花时间创建并添加一个简单的5:00PM,例如在注释中。有没有办法通过我的查询解析时间而不是日期并将其设置为MKAnnotation

override func viewDidAppear(animated: Bool) {
var annotationQuery = PFQuery(className: "Post")
currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
//annotationQuery.whereKey("Location", nearGeoPoint: currentLoc, withinMiles: 10)
annotationQuery.whereKeyExists("Location")
annotationQuery.findObjectsInBackgroundWithBlock {
    (points, error) -> Void in
    if error == nil {
        // The find succeeded.
        println("Successful query for annotations")
        // Do something with the found objects

        let myPosts = points as! [PFObject]

        for post in myPosts {
            let point = post["Location"] as! PFGeoPoint
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
            annotation.title = post["title"] as! String!
            annotation.subtitle = post["username"] as! String!


            self.mapView.addAnnotation(annotation)
        }

    } else {
        // Log details of the failure
        println("Error: \(error)")
    }
}

这是我的 Parse 后端的照片,可以让您更好地了解:

http://i1249.photobucket.com/albums/hh512/zachkhan3/Screen%20Shot%202015-04-24%20at%205.39.18%20PM.png

【问题讨论】:

  • 您需要检索“createadAt”字段并使用 NSDateFormatter 对其进行格式化。
  • 谢谢泰迪!我会尝试解决它,如果它成功了,我会告诉你。
  • 当我检索“createdAt”时,是否将其存储为字符串?
  • 不,不。只需使用这个:让 createdAt = post.createdAt。 CreatedAt 不是“真实字段”,而是对象本身的属性。它返回一个 NSDate。

标签: ios xcode swift time parse-platform


【解决方案1】:

PFObject 有一个“CreatedAt”属性。您将其分配给 NSDate 对象,您需要使用 NSDateFormatter 对其进行格式化以仅显示时间组件。为您提供未经测试的代码。

       for post in myPosts {
        let point = post["Location"] as! PFGeoPoint
        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
        annotation.title = post["title"] as! String!
        annotation.subtitle = post["username"] as! String!
        let formatter = NSDateFormatter()
        formatter.dateStyle = NSDateFormatterStyle.NoStyle
        formatter.timeStyle = .ShortStyle

        let dateString = formatter.stringFromDate(post.createdAt)
        annotation.timeTitle /* or something equivalent */ = dateString

        self.mapView.addAnnotation(annotation)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2017-06-11
    • 2010-10-26
    • 2014-03-18
    • 2021-10-24
    相关资源
    最近更新 更多