0

緯度と経度を取得しようとしています。実際のデバイスで次のコードを実行していますが、緯度と経度が常にゼロになります。ライブラリもインポートし、デリゲートも設定します。何が間違っていて、どうすればよいか教えてもらえますか? 私のデバイスにはios 8があります。

CLLocationManager *locationManager = [[CLLocationManager alloc] init];

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


CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];

NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(@"%@",str);
4

1 に答える 1

2

デリゲートメソッドで現在の場所を取得する必要があります

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations objectAtIndex:0];
}
于 2014-10-21T09:40:26.863 に答える