String to CLLocation latitude and Longitude

扶醉桌前 提交于 2019-12-20 09:00:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!