在 Swift 中将 CLLocationCoordinate2D 转换为 CLLocation 时出错
Error converting CLLocationCoordinate2D to CLLocation in Swift
我正在尝试将 cllocationcoordinate2d 转换为 cllocation。我在下面代码的第 12 行收到错误 "Expected expression in container literal"。我还在第 13 行收到错误 "Cannot convert value of type cllocationcoordinate2d into expected value cllocation",但那是因为第 12 行无法正常工作。
@IBAction func makeEvent(sender: UIButton)
{
let center = CLLocationCoordinate2D(latitude: loc1.coordinate.latitude, longitude: loc1.coordinate.longitude)
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
self.pointAnnotation1 = MKPointAnnotation()
self.pointAnnotation1.title = "Event"
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!)
CLLocation *center = [[CLLocation alloc] initWithLatitude:latt longitude:longg]
eventRecord.setObject(center, forKey: "event")
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(eventRecord) { record, error in
}
if error == nil
{
print("Location saved")
}
loadEvent(){ (error, records) in
if error != nil {
print("error fetching locations")
} else {
print("Found Event")
}
}
}
您混淆了 Objective-C 和 Swift。
试试这个:
let center = CLLocation(latitude: lat, longitude: long)
而不是:
CLLocation *center = [[CLLocation alloc] initWithLatitude:latt longitude:longg]
我正在尝试将 cllocationcoordinate2d 转换为 cllocation。我在下面代码的第 12 行收到错误 "Expected expression in container literal"。我还在第 13 行收到错误 "Cannot convert value of type cllocationcoordinate2d into expected value cllocation",但那是因为第 12 行无法正常工作。
@IBAction func makeEvent(sender: UIButton)
{
let center = CLLocationCoordinate2D(latitude: loc1.coordinate.latitude, longitude: loc1.coordinate.longitude)
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
self.pointAnnotation1 = MKPointAnnotation()
self.pointAnnotation1.title = "Event"
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!)
CLLocation *center = [[CLLocation alloc] initWithLatitude:latt longitude:longg]
eventRecord.setObject(center, forKey: "event")
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(eventRecord) { record, error in
}
if error == nil
{
print("Location saved")
}
loadEvent(){ (error, records) in
if error != nil {
print("error fetching locations")
} else {
print("Found Event")
}
}
}
您混淆了 Objective-C 和 Swift。
试试这个:
let center = CLLocation(latitude: lat, longitude: long)
而不是:
CLLocation *center = [[CLLocation alloc] initWithLatitude:latt longitude:longg]