我看过类似的问题和示例,但似乎无法从CoreLocation获取纬度和经度,因为如果我直接在viewDidLoad中查看位置,则它表示为0/0。我在访问位置的委托方法中有一个断点,但我从未遇到过。我也符合CLLocationmanagerDelegate。

代码(ViewController.m):

- (void)viewDidLoad
{
    [super viewDidLoad];

    _locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    _locationManager.distanceFilter = kOneMile; //custom defined this one
    [_locationManager startUpdatingLocation];
}

//Actual update of the location
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //Not sure if this is the best way to grab it?
    CLLocation *location = [locations objectAtIndex:0];

    //Breakpoint shows this as zero here
    [Utilities GetCityAndState:location.coordinate.latitude andLong:location.coordinate.longitude];
}

在我的实用程序 class 中
+(NSString *)GetCityAndState:(float)withlat andLong:(float)longitude{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
NSString *urlString = @"http://maps.googleapis.com/maps/api/geocode/json?latlng=";
//[urlString ] I am going to append the lat and lont to the urlString here
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:urlString]];
NSHTTPURLResponse *responseCode = nil;
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
NSString *get = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

//Once I have that I am going to take the results from the reverse geocoding and return a string like LasAngelesCA
}

最佳答案

我不完全确定为什么,但是在模拟器中删除该应用程序并重新运行它可以解决此问题。

10-06 02:01