tho*_*mas 12 string iphone latitude-longitude
一个小的转换问题让我发疯.
我有一个代表纬度的字符串(ex"35.453454").我想用它作为CLLocation的纬度.
如何以正确的CLLocation(度)格式转换字符串?
非常感谢,这让我很生气!托马斯
ken*_*ytm 51
要将NSString转换为CLLocationDegrees(即double
):
return [theString doubleValue];
Run Code Online (Sandbox Code Playgroud)
让我们假设您已将字符串存储"35.453454"
为
在目标C中
NSString *latitudeString = @"35.453454";
在Swift 2.2中
let latituteString : String = "35.453454"
Run Code Online (Sandbox Code Playgroud)
并且您希望将此NSString转换为正确的CLLocation.
但CLLocation分别有两个参数纬度和经度.
除非您没有与您的给定纬度相对应的经度"35.453454",否则无法在CLLocation中存储您的纬度.
情况1:假设您没有相应的经度.然后,您可以暂时将您的唯一纬度存储在CLLocationDegrees中,以便稍后在初始化CLLocation对象时使用它.
在Objective-C中:
CLLocationDegress myLatitude = [latitudeString doubleValue];
Run Code Online (Sandbox Code Playgroud)
在Swift 2.2中
let myLatitute : CLLocationDegress = Double(latitudeString)
Run Code Online (Sandbox Code Playgroud)
情况2:假设您有相应的经度.然后,您可以将您的纬度和经度存储在CLLocationDegrees中,以便在初始化CLLocation对象时使用它.
让你的经度 18.9201344
然后,
在Objective-C中
NSString *longitudeString = @"18.9201344";
//creating latitude and longitude for location
CLLocationDegrees latitudeDegrees = [latitudeString doubleValue];
CLLocationDegrees longitudeDegrees = [longitudeString doubleValue];
//initializing location with respective latitude and longitude
CLLocation *myLocation = [[CLLocation alloc]initWithLatitude:latitudeDegrees longitude:longitudeDegrees];
Run Code Online (Sandbox Code Playgroud)
在Swift 2.2中
let longitudeString : String = "18.9201344"
let latitudeDegrees : CLLocationDegrees = Double(latitudeString)
let longitudeDegrees : CLLocationDegress = Double(longitudeString)
let location : CLLocation = CLLocation.init(latitude: latitudeDegrees, longitude: longitudeDegrees)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12827 次 |
最近记录: |