问题
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?
回答1:
I think you need to:
LocationAtual.coordinate.latitude = [@"-56.6462520" floatValue];
LocationAtual.coordinate.longitude = [@"-36.6462520" floatValue];
回答2:
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];
回答3:
Swift Answer for the lazy:
var LocationAtual: CLLocation = CLLocation(latitude: -56.6462520, longitude: -36.6462520)
回答4:
CLLocation coordinate is actually a read only value
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
So the best way to assign dummy data to coordinates is AMITp way
回答5:
lattitude and longitude are double values so need to be assigned this way.
CLLocation *LocationAtual=[[CLLocation alloc] initWithLatitude:[[location objectForKey:@"latitude"] doubleValue] longitude:[[location objectForKey:@"longitude"] doubleValue]]
来源:https://stackoverflow.com/questions/7907734/string-to-cllocation-latitude-and-longitude