One small conversion problem that drives me crazy.
I have a string (ex "35.453454") that represents a latitude. I want to use it as a latitude for CLLocation.
How can I convert the string in the proper CLLocation (in degrees) format ?
Many thanks, this drives me so mad ! Thomas
To convert an NSString to CLLocationDegrees (which is double
):
return [theString doubleValue];
Lets assume that you have stored your string "35.453454"
as
In Objective C
NSString *latitudeString = @"35.453454";
In Swift 2.2
let latituteString : String = "35.453454"
And you want to convert this NSString to proper CLLocation.
But CLLocation has two parameter latitude and longitude respectively.
Unless, you don't have a longitude corresponding to your given latitude "35.453454" is it not possible to store just you latitude in CLLocation.
CASE 1 : Assume that you don't have corresponding longitude. Then, you can store your only latitude in CLLocationDegrees for the time being to later use it in initializing your CLLocation object.
In Objective-C :
CLLocationDegress myLatitude = [latitudeString doubleValue];
In Swift 2.2
let myLatitute : CLLocationDegress = Double(latitudeString)
CASE 2 : Assume that you have corresponding longitude. Then, you can store your both latitude and longitude in CLLocationDegrees to use it in initializing your CLLocation object.
Let your longitude be 18.9201344
then,
In 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];
In 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With