【问题标题】:Converting CLLocationCoordinate2d to Street Address with Swift使用 Swift 将 CLLocationCoordinate2d 转换为街道地址
【发布时间】:2018-08-18 06:19:33
【问题描述】:

我正在尝试将 CLLocationCoordinates2d 坐标转换为实际街道地址。

我尝试过 CLGeocoder,但没有成功。这是我提供纬度和经度坐标的代码。

    let manager = CLLocationManager()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")
    // let locationtext = "locations = \(locValue.latitude) \(locValue.longitude)"
    locationTxt.text = "\(locValue.latitude) \(locValue.longitude)"

}

谢谢!

【问题讨论】:

  • 请向我们展示您使用CLGeocoder的代码。

标签: swift xcode geocoding reverse cllocation


【解决方案1】:
func convertLatLongToAddress(latitude:Double,longitude:Double){
    
    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: latitude, longitude: longitude)
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
        
        // Place details
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]
        
        // Location name
        if let locationName = placeMark.location {
            print(locationName)
        }
        // Street address
        if let street = placeMark.thoroughfare {
            print(street)
        }
        // City
        if let city = placeMark.locality {
            print(city)
        }
        // State
        if let state = placemark.administrativeArea {
            print(state)
        }
        // Zip code
        if let zipCode = placeMark.postalCode {
            print(zipCode)
        }
        // Country
        if let country = placeMark.country {
            print(country)
        }
    })
    
}

【讨论】:

  • 感谢您的快速回复!所以我在地理编码器和位置上遇到错误。 geoCoder CLGeocoder 0x00006040000171e0 和位置 CLLocation 0x00006040000170f0
  • @klykins 哪个错误以及您在我的代码中出现错误的哪一行让我知道,所以我会再次检查它
  • 第 1 行和第 2 行是否需要导入其他内容?导入 MapKit 导入 CoreLocation
  • @klykins 是的,当然你需要导入 CoreLocation
  • 我目前正在导入那个
【解决方案2】:

是的,您可以从坐标中获取街道地址,因为您需要以下代码

func getAddressFromLatLon(Latitude: Double, Longitude: Double) {
    var center : CLLocationCoordinate2D = CLLocationCoordinate2D()


    let ceo: CLGeocoder = CLGeocoder()
    center.latitude = Latitude
    center.longitude = Longitude

    let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)


    ceo.reverseGeocodeLocation(loc, completionHandler:
        {(placemarks, error) in
            if (error != nil)
            {
                print("reverse geodcode fail: \(error!.localizedDescription)")
            }
            let pm = placemarks! as [CLPlacemark]

            if pm.count > 0 {
                let pm = placemarks![0]
                print(pm.country)
                print(pm.locality)
                print(pm.subLocality)
                print(pm.thoroughfare)
                print(pm.postalCode)
                print(pm.subThoroughfare)
                var addressString : String = ""
                if pm.subLocality != nil {
                    addressString = addressString + pm.subLocality! + ", "
                }
                if pm.thoroughfare != nil {
                    addressString = addressString + pm.thoroughfare! + ", "
                }
                if pm.locality != nil {
                    addressString = addressString + pm.locality! + ", "
                }
                if pm.country != nil {
                    addressString = addressString + pm.country! + ", "
                }
                if pm.postalCode != nil {
                    addressString = addressString + pm.postalCode! + " "
                }


                print(addressString)
          }
    })

}

调用该函数并传递参数可以得到地址字符串。如果不起作用,请发表评论,以便我进一步指导您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多