试图在iOS中检索当前位置

时间:2013-06-22 06:47:16

标签: ios cllocationmanager cllocation

我正在构建一个应用程序,要求我找到指定位置与当前位置之间的距离。 虽然我在模拟器中指定了自定义位置,但我使用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);

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

1 个答案:

答案 0 :(得分: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);