字符串到CLLocation纬度和经度

时间:2021-11-14 15:42:17

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:

我有两个代表纬度和经度的字符串,如:“ - 56.6462520”,我想分配给一个CLLocation对象来比较我当前的位置。我尝试了下面的代码,但我只得到错误:

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?

然后将对象与我的实际位置纬度和经度进行比较。有什么建议么?

5 个解决方案

#1


12  

I think you need to:

我想你需要:

LocationAtual.coordinate.latitude = [@"-56.6462520" floatValue];
LocationAtual.coordinate.longitude = [@"-36.6462520" floatValue];

#2


105  

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

您不能直接分配到坐标 - 它是CLLocation的只读属性。使用以下实例方法:

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

example:

例:

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

#3


7  

Swift Answer for the lazy:

快速回答懒惰:

var LocationAtual: CLLocation = CLLocation(latitude: -56.6462520, longitude: -36.6462520)

#4


2  

CLLocation coordinate is actually a read only value

CLLocation坐标实际上是只读值

    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

So the best way to assign dummy data to coordinates is AMITp way

因此,将哑元数据分配给坐标的最佳方法是AMITp方式

#5


1  

lattitude and longitude are double values so need to be assigned this way.

lattitude和longitude是double值,因此需要以这种方式分配。

CLLocation *LocationAtual=[[CLLocation alloc] initWithLatitude:[[location objectForKey:@"latitude"] doubleValue] longitude:[[location objectForKey:@"longitude"] doubleValue]]

#1


12  

I think you need to:

我想你需要:

LocationAtual.coordinate.latitude = [@"-56.6462520" floatValue];
LocationAtual.coordinate.longitude = [@"-36.6462520" floatValue];

#2


105  

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

您不能直接分配到坐标 - 它是CLLocation的只读属性。使用以下实例方法:

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

example:

例:

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

#3


7  

Swift Answer for the lazy:

快速回答懒惰:

var LocationAtual: CLLocation = CLLocation(latitude: -56.6462520, longitude: -36.6462520)

#4


2  

CLLocation coordinate is actually a read only value

CLLocation坐标实际上是只读值

    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

So the best way to assign dummy data to coordinates is AMITp way

因此,将哑元数据分配给坐标的最佳方法是AMITp方式

#5


1  

lattitude and longitude are double values so need to be assigned this way.

lattitude和longitude是double值,因此需要以这种方式分配。

CLLocation *LocationAtual=[[CLLocation alloc] initWithLatitude:[[location objectForKey:@"latitude"] doubleValue] longitude:[[location objectForKey:@"longitude"] doubleValue]]