2011-10-26 91 views
20

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

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

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

+6

Objective-C的惯例是变种的名称驼峰。 –

回答

11

我想你需要:

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

我明白了,我认为这个转换是正确的,但是我得到这个错误:“Lvaule需要作为赋值的左操作数”... –

+1

http://stackoverflow.com/questions/5527878/lvalue-required-as-左操作数转让 –

+2

我无法删除接受的答案,但同意AmitP的答案是正确的。我的回答是“你不能使用字符串作为数字” –

101

您不能直接分配到协调 - 这是CLLocation的只读属性。
使用下面的实例方法:

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

例如:

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

这是真的,你不能分配给CLLocation属性。 –

+0

上一个答案不正确。这是正确的。 – hook38

2

CLLocation坐标实际上是一个只读值

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

所以到虚拟数据分配到坐标的最好办法是AMITp方式

1

地位和longitu de是双重值,所以需要这样分配。

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

懒惰的雨燕答:

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

懒惰谢谢你。 :d – PruitIgoe

相关问题