同様の質問と例を見てきましたが、viewDidLoadで場所を直接見ると0/0であると表示されているため、CoreLocationから緯度と経度を取得できないようです。場所にアクセスするためのデリゲート メソッドにブレークポイントがありますが、ヒットしたことはありません。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];
}
私のユーティリティクラスで
+(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
}