xcode 将字符串转换为 CLLocationCoordinate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27590659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to CLLocationCoordinate
提问by Pierre Olivier Tran
Is it possible to convert a string to a longitude/latitude value? I managed to convert the coordinate to a string, but I cannot find a way to revert the process
是否可以将字符串转换为经度/纬度值?我设法将坐标转换为字符串,但找不到恢复过程的方法
回答by sanjana
Another way to convert:
另一种转换方式:
let latitude = (latitudeString as NSString).doubleValue
let longitude = (longitudeString as NSString).doubleValue
回答by kiran pm
Swift 3
斯威夫特 3
let lat = Double(String1)
let lon = Double(String2)
let coordinates = CLLocationCoordinate2D(latitude:lat!
, longitude:lon!)
CLLocationCoordinate2D
is a double value it can convert string into double See in above example
CLLocationCoordinate2D
是一个双精度值它可以将字符串转换为双精度见上例
回答by Pierre Olivier Tran
My bad, it was a simple type. If anyone ever struggle on how to convert a string to coordinates, here's the correct syntax :
我的不好,这是一个简单的类型。如果有人在如何将字符串转换为坐标方面遇到困难,这里是正确的语法:
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: Double(([longitudeString] as NSString).doubleValue), longitude: Double(([LatitudeString] as NSString).doubleValue))
回答by Anji Mendpara
var currentLocationLatitude = "45.5626" // your string latitude
var currentLocationLongitude = "45.5626" // your string longitude
var currentLocation:CLLocationCoordinate2D! //location object
currentLocation = CLLocationCoordinate2D(latitude:currentLocationLatitude.toDouble() ?? 0.0, longitude: currentLocationLongitude.toDouble() ?? 0.0)
extension String
{
/// EZSE: Converts String to Double
public func toDouble() -> Double?
{
if let num = NumberFormatter().number(from: self) {
return num.doubleValue
} else {
return nil
}
}
}
You can take string latitude and longitude from you API response and in CLLocationCoordinate2D variable, pass with converting to Double value. I have also added extension for converting string to double.
您可以从 API 响应和 CLLocationCoordinate2D 变量中获取字符串纬度和经度,并通过转换为 Double 值传递。我还添加了将字符串转换为双精度的扩展。
回答by g89n7man
might help
可能有帮助
let Latitude = ("41" as NSString).doubleValue
let Longitude = ("29" as NSString).doubleValue