如何提取lat / long placemark.coordinate swift

时间:2017-02-04 17:10:34

标签: ios swift google-maps

在我的应用中添加了位置搜索栏 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)

控制台输出:

enter image description here

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

也许你的想法太难了。您可以像其他任何一样为这些变量赋值:

let (destLat, destLong) = (placemark.coordinate.latitude, placemark.coordinate.longitude)

答案 1 :(得分:1)

如果您声明的两个变量都是Double的类型,那么您只需要访问latitude的{​​{1}}和longitude属性。

CLLocationCoordinate2D

如果var的类型不同,则只需将其更改为destLat = destination.latitude destLong = destination.longitude 即可。