Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iphone. Create a latitude from a string or integers

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

like image 258
thomas Avatar asked Apr 19 '10 08:04

thomas


2 Answers

To convert an NSString to CLLocationDegrees (which is double):

return [theString doubleValue];
like image 187
kennytm Avatar answered Oct 06 '22 00:10

kennytm


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)
like image 36
aashish tamsya Avatar answered Oct 05 '22 23:10

aashish tamsya