gpt4 book ai didi

xcode - 如何在 Xcode 中将 CLLocation 转换为字符串

转载 作者:搜寻专家 更新时间:2023-11-01 06:06:35 25 4
gpt4 key购买 nike

我使用 SwiftLocation 获取坐标:

try! SwiftLocation.shared.currentLocation(Accuracy.House, timeout: 20, onSuccess: { (location) -> Void in
print(location?.description)
}) { (error) -> Void in
print("something went wrong")
}

在位置?.描述我明白了:

<+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 2/22/16, 6:35:55 PM Indochina Time

我只需要获取坐标。所以我为它循环:

    while name.characters.last != ">" { // delete all after ">"
name = String(name.characters.dropLast())
}
name = String(name.characters.dropLast()) // delete ">"
name = String(name.characters.dropFirst()) // delete "<"
name = String(name.characters.dropFirst()) // delete "+"
print(name) // get 37.78583400,-122.40641700
tempMyLat = name
while tempMyLat.characters.last != "," { // delete all after ","
tempMyLat = String(tempMyLat.characters.dropLast())
}
tempMyLon = name
while tempMyLon.characters.first != "," { // delete all before ","
tempMyLon = String(tempMyLon.characters.dropFirst())
}
tempMyLon = String(tempMyLon.characters.dropFirst()) // delete ","

但此代码仅适用于字符串。当我得到 location?.description - 我无法将它转换为类型 - 字符串,因为“location”是 CLLocation 类型。

那么,我的问题是:如何将 location?.description 转换为 String ? 或者 如何仅从 SwiftLocation 获取坐标

谢谢。

最佳答案

在 Xcode 10+ 和 Swift 3 中,您可能必须执行类似于以下操作的操作:

let myLocation: CLLocation = locations[0] as CLLocations
var myLatitude: String = String(format: "%f", myLocation.coordinate.latitude)
var myLongitude: String = String(format:"%f", myLocation.coordinate.longitude)

关于xcode - 如何在 Xcode 中将 CLLocation 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com