经度和纬度值均为0.000000,为什么?

时间:2011-04-29 22:29:10

标签: ios core-location

我使用核心位置获取当前位置的经度和纬度值,并将其显示在标签中,因此对于我的情况,经度和纬度都是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];

2 个答案:

答案 0 :(得分:3)

只是初始化位置管理器无法获取值。

你必须写这个

[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];
}

希望有所帮助!

  • Shuanak

答案 1 :(得分:0)

没有。 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需要时间来实现目标。这在设备上确实如此,但也许在模拟器上也是如此。