通过Parse向MapView Annotation添加时间戳?

时间:2015-04-25 02:29:22

标签: ios xcode swift time parse-platform

这是我当前的查询设置,用于从解析中检索数据并将其设置为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

1 个答案:

答案 0 :(得分: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)
    }