String to CLLocation latitude and Longitude

Vinicius Albino picture Vinicius Albino · Oct 26, 2011 · Viewed 51.2k times · Source

I have two strings representing latitude and longitude like: "-56.6462520", and i want to assign then to a CLLocation object to compare to my current location.I tried the following code but i get errors only:

CLLocation * LocationAtual = [[CLLocation alloc]init];
LocationAtual.coordinate.latitude = @"-56.6462520";
LocationAtual.coordinate.longitude = @"-36.6462520";

and then compare the object with my actual location latitude and longitude. Any suggestions?

Answer

AmitP picture AmitP · Feb 11, 2013

You cannot assign directly into coordinate - it is a readonly property of CLLocation.
Use the following instance method:

- (instancetype)initWithLatitude:(CLLocationDegrees)latitude
                       longitude:(CLLocationDegrees)longitude

example:

CLLocation *LocationAtual = [[CLLocation alloc] initWithLatitude:-56.6462520 longitude:-36.6462520];