经度和纬度值都是 0.000000 ,为啥?

Posted

技术标签:

【中文标题】经度和纬度值都是 0.000000 ,为啥?【英文标题】:longitude and latitude values are both 0.000000 , why?经度和纬度值都是 0.000000 ,为什么? 【发布时间】:2011-04-29 22:29:09 【问题描述】:

我使用核心位置来获取我当前位置的经度和纬度值,并将其显示在标签中,所以对于我来说,经度和纬度始终为 0.000000,是因为我在模拟器上工作吗?

    //longitude and latitude
    // locationManager update as location
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];

    CLLocation *location = [locationManager location];

    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

【问题讨论】:

你在加纳海岸附近的船上吗? 【参考方案1】:

仅初始化位置管理器不会获取您的值。

你必须写这个

[self.locationManager startUpdatingLocation];

那么你必须重写一个回调方法

- (void) locationManager:(CLLocationManager *)manager 
     didUpdateToLocation:(CLLocation *) newLocation
    fromLocation: (CLLocation *) oldLocation 


    [self.locationManager stopUpdatingLocation];
     NSString *latitude = [NSString stringWithFormat:@"%f", newLocation.latitude]; 
         NSString *longitude = [NSString stringWithFormat:@"%f", newLocation.longitude];  
    [self.locationManager release];

希望有帮助!

舒纳克

【讨论】:

嗨,这对我没有帮助,标签中显示了相同的值:((【参考方案2】:

没有。 iPhone 模拟器上的 CoreLocation 固定在 37.3317 North,122.0307 West(Apple HQ)。

尝试将您的 locationManager 代码从带有标签的代码中移开。将位置管理器代码放在viewDidLoad之类的里面然后把这段代码放上去

CLLocation *location = [locationManager location];

// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];

NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];  

在您从按钮调用的函数中。 CoreLocation 需要时间来让自己运转起来。在设备上确实如此,但在模拟器上也许也是如此。

【讨论】:

以上是关于经度和纬度值都是 0.000000 ,为啥?的主要内容,如果未能解决你的问题,请参考以下文章