尽管 CLLocationDegrees 有值,但它返回 0

发布于 2024-10-25 19:33:54 字数 617 浏览 4 评论 0原文

我有一个包含 CLLocationCooperative2D 的类。 我得到了纬度&通过网络请求从网络获取经度。

当我使用 CLLocationCooperative2D 对象将对象放在地图上 (MKMapView) 时,一切正常。

但是,当我尝试将 CLLocationCooperative2D 的纬度或经度 (CLLocationDegrees) 与其他变量进行比较或将其放入字符串中时,我总是得到 0(零),尽管它的值是正确的(35.0333..等)

例如:

item.coordinate.latitude 是 32.816326141357422

(当我在调试时将光标放在项目上时显示,并且在将对象放在地图上时也会正确显示)

NSLog(@"lat: %f", item.coordinate.latitude) ; =>这个输出“lat: 0”

NSString* lats = [[NSNumber numberWithDouble:item.coordinate.latitude] stringValue]; =>这个lats变成“0”。

有人知道如何解决这个问题吗? 谢谢。

I have a class that holds a CLLocationCoordinate2D.
I get the latitude & longitude from the web by a web request.

When I'm putting an object on a map (MKMapView), using the CLLocationCoordinate2D object, everything works fine.

But, when I'm trying to compare the latitude OR longitude (CLLocationDegrees) of the CLLocationCoordinate2D to other variables or putting it into a string, I always get 0 (zero), although it value is correct (35.0333.., etc.)

For example:

item.coordinate.latitude is 32.816326141357422

(shown when I put my cursor on the item while debugging, and also correct when putting an object on the map)

NSLog(@"lat: %f", item.coordinate.latitude); => this one outputs "lat: 0"

NSString* lats = [[NSNumber numberWithDouble:item.coordinate.latitude] stringValue]; => this one lats becomes "0".

Does anyone know how to solve this issue?
thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2024-11-01 19:33:54

而不是

NSLog(@"lat: %f", item.coordinate.latitude);

尝试:

NSLog(@"lat: %+.6f", item.coordinate.latitude);

我使用类似于以下内容的内容:

if (![[NSString stringWithFormat:@"%+.6f,%+.6f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude] isEqualToString:@"+0.000000,+0.000000"])
{
    // do cool stuff
}

另外,请确保您已为 CLLocationManager 设置委托和委托方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    if (locationManager.locationServicesEnabled)
        [locationManager startUpdatingLocation];
}

#pragma mark -
#pragma mark CLLocationManager Delegate Methods

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0)
    {
        NSLog(@"New Latitude %+.6f, Longitude %+.6f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    }
}

Instead of

NSLog(@"lat: %f", item.coordinate.latitude);

try:

NSLog(@"lat: %+.6f", item.coordinate.latitude);

I use something similar to the following:

if (![[NSString stringWithFormat:@"%+.6f,%+.6f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude] isEqualToString:@"+0.000000,+0.000000"])
{
    // do cool stuff
}

Also, make sure you have set the delegate and the delegate methods for your CLLocationManager:

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    if (locationManager.locationServicesEnabled)
        [locationManager startUpdatingLocation];
}

#pragma mark -
#pragma mark CLLocationManager Delegate Methods

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0)
    {
        NSLog(@"New Latitude %+.6f, Longitude %+.6f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文