如何使用NSDecimalNumber在IOS中转换纬度和经度

时间:2014-12-26 09:52:02

标签: ios objective-c mapkit cllocation nsdecimalnumber

我想在地图上显示我的位置,我正在使用CLLocation来执行此操作

这是我的代码:

CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;

latitudelongitude是浮点类型并从数据库获取 但是,当我转换为浮动值,所以我得到错误的值:

float latitude = [myLatString floatValue];

和结果:

  

myLatString:10.861714

     

浮动值:10.8617144

所以我无法使用上述值加载到我的mapview。

我在google上搜索过,有人说使用NSDecimalNumber但是如何使用NSDecimalNumber工作CLLocation ??

请帮帮我

2 个答案:

答案 0 :(得分:1)

使用NSDecimalNumberNSString转换为float

NSString *myLatString = @"10.8617144";
NSDecimalNumber *decimalPoint = [[NSDecimalNumber alloc] initWithString: myLatString];
float latitude = decimalPoint.floatValue;

答案 1 :(得分:1)

使用双打。纬度和经度为CLLocationDegree,为双倍。

最好使用CLLocationCoordinate2DMake

CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([myLatString doubleValue], [myLngString doubleValue]);