我查看了类似的问题和示例,但我似乎无法从 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];
}
在我的实用工具类
+(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
}