-1

我正在构建一个应用程序,它需要我找到指定位置和我当前位置之间的距离。我使用CLLocationManager的是相同的,但检索到的坐标是 0,0,尽管我在模拟器中指定了自定义位置。这是代码:.m 文件

@property (nonatomic, strong) CLLocationManager *locationManager2;
...
@synthesize locationManager2;
- (CLLocationManager *)locationManager2 {

if (locationManager2 != nil) {
    return locationManager2;
}

locationManager2 = [[CLLocationManager alloc] init];
[locationManager2 setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager2 setDelegate:self];

return locationManager2;
}
 ...
 ...
 ...
  CLLocation *locationHome = [locationManager2 location];

NSLog(@"%f",locationHome.coordinate.latitude);

纬度记录为零。我的代码有什么问题吗?

4

1 回答 1

0

输入这段代码,让我知道你在控制台得到了什么。

此代码基于 iOS 6 构建。

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}

location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];;
MKCoordinateRegion region;
region.center=coordinate;
MKCoordinateSpan span;
span.latitudeDelta=10.015;
span.longitudeDelta=10.015;
region.span=span;
[mapView setRegion:region];

NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(@"%@",str);
于 2013-06-22T10:50:37.707 回答