首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中提取lat/long placemark.coordinate?

如何在Swift中提取lat/long placemark.coordinate?
EN

Stack Overflow用户
提问于 2017-02-04 17:10:33
回答 2查看 1.1K关注 0票数 0

我在我的应用程序中添加了一个位置搜索栏。tableview控制器包括用户位置lat/long以及目标(将用户输入到搜索栏中的地址)。据我所知,纬度/经度在placemark.coordinate内:

代码语言:javascript
复制
placemark.coordinate = CLLocationCoordinate2D(latitude: 45.253519203737767, longitude: -66.070974382763978)

我有两个变量:destLatdestLong。如何从placemark.coordinate中提取纬度和经度,并将其放入上述两个变量中?

相关代码:

代码语言:javascript
复制
// 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)

控制台输出:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-04 17:17:42

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

代码语言:javascript
复制
let (destLat, destLong) = (placemark.coordinate.latitude, placemark.coordinate.longitude)
票数 1
EN

Stack Overflow用户

发布于 2017-02-04 17:17:49

如果您声明的两个变量都是Double类型,那么只需访问CLLocationCoordinate2Dlatitudelongitude属性即可。

代码语言:javascript
复制
destLat = destination.latitude
destLong = destination.longitude

如果这两个var的类型不同,那么只需将其更改为Double即可。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42043145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档