通过解析将时间戳添加到 MapView 注释?
Adding timestamp to MapView Annotation via Parse?
这是我当前的查询设置,用于从解析中检索数据并将其设置到 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 后端的照片,可以让您更好地了解:
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)
}
这是我当前的查询设置,用于从解析中检索数据并将其设置到 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 后端的照片,可以让您更好地了解:
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)
}