我在我的应用程序中添加了一个位置搜索栏。tableview控制器包括用户位置lat/long以及目标(将用户输入到搜索栏中的地址)。据我所知,纬度/经度在placemark.coordinate内:
placemark.coordinate = CLLocationCoordinate2D(latitude: 45.253519203737767, longitude: -66.070974382763978)我有两个变量:destLat和destLong。如何从placemark.coordinate中提取纬度和经度,并将其放入上述两个变量中?
相关代码:
// cache the pin
selectedPin = placemark
// clear existing pins
mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = placemark.coordinate
annotation.title = placemark.name
if let city = placemark.locality,
let prov = placemark.administrativeArea {
annotation.subtitle = "\(city), \(prov)"
}
let destination = placemark.coordinate
print("Destination coordinates: \(destination)")
let name = placemark.name
print("Placemark Name: \(name!)")
mapView.addAnnotation(annotation)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegionMake(placemark.coordinate, span)
mapView.setRegion(region, animated: true)控制台输出:

发布于 2017-02-04 17:17:42
也许你想的太多了。您可以像其他变量一样为这些变量赋值:
let (destLat, destLong) = (placemark.coordinate.latitude, placemark.coordinate.longitude)发布于 2017-02-04 17:17:49
如果您声明的两个变量都是Double类型,那么只需访问CLLocationCoordinate2D的latitude和longitude属性即可。
destLat = destination.latitude
destLong = destination.longitude如果这两个var的类型不同,那么只需将其更改为Double即可。
https://stackoverflow.com/questions/42043145
复制相似问题