我希望图钉出现在地图上,注释位于存储在cloudkit中的位置。正在保存和查询记录,但屏幕上没有任何内容。当您最初创建活动时,它会显示在屏幕上,直到应用重新启动。
@IBAction func makeEvent(sender: UIButton)
{
var title2 = String()
let alertController = UIAlertController(title: "Event?", message: "Please name your event:", preferredStyle: .Alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .Default) { (_) in
if let field = alertController.textFields![0] as? UITextField {
NSUserDefaults.standardUserDefaults().setObject(field.text, forKey: "userEvent")
NSUserDefaults.standardUserDefaults().synchronize()
let center = CLLocationCoordinate2D(latitude: self.loc1.coordinate.latitude, longitude: self.loc1.coordinate.longitude)
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
self.pointAnnotation1 = MKPointAnnotation()
self.pointAnnotation1.title = (field.text)
title2 = field.text!
self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil)
self.mapView?.centerCoordinate = self.pointAnnotation1.coordinate
self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
let center1 = CLLocation(latitude: self.loc1.coordinate.latitude, longitude: self.loc1.coordinate.longitude)
self.eventRecord.setObject(center1, forKey: "event")
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(self.eventRecord) { record, error in
}
if self.error == nil
{
print("Event saved")
}
} else {
}
self.titleRecord.setObject(title2, forKey: "titles")
let publicData1 = CKContainer.defaultContainer().publicCloudDatabase
publicData1.saveRecord(self.titleRecord) { record, error in
}
if self.error == nil
{
print("Title saved")
}
}
func drawEvents(loc: CLLocation, title1: String)
{
let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
self.pointAnnotation1 = MKPointAnnotation()
self.pointAnnotation1.title = title1
self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil)
self.mapView?.centerCoordinate = self.pointAnnotation1.coordinate
self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
}
func loadEvent(completion: (error:NSError?, records:[CKRecord]?) -> Void)
{
let query = CKQuery(recordType: "Event", predicate: NSPredicate(value: true))
CKContainer.defaultContainer().publicCloudDatabase.performQuery(query, inZoneWithID: nil){
(records, error) in
if error != nil {
print("error fetching events: \(error)")
completion(error: error, records: nil)
} else {
//print("found events: \(records)")
print("found events")
completion(error: nil, records: records)
guard let records = records else {
return
}
let query1 = CKQuery(recordType: "Title", predicate: NSPredicate(value: true))
CKContainer.defaultContainer().publicCloudDatabase.performQuery(query1, inZoneWithID: nil){
(records1, error) in
if error != nil {
print("error fetching titles: \(error)")
completion(error: error, records: nil)
} else {
//print("found event title: \(records1)")
print("found event title")
completion(error: nil, records: records1)
guard let records1 = records1 else {
return
}
//for record in records
for(var i = 0; i<records.count; i += 1)
{
for(var j = i; j<i+1; j += 1)
{
self.drawEvents(records[i]["event"] as! CLLocation, title1: records1[i]["titles"] as! String)
print("drawing events")
}
}
}
}
}
}
}