开发者

CLLocationDegrees returns 0 although it has a value

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 CLLocat开发者_Python百科ionCoordinate2D 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.


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜