我正在尝试使用纬度和经度通过CLLocationManager获取City。 城市名称得到一些时间,而不是得到一些时间,所以我怎么能解决这个问题。
-(Void)ViewDidload
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *location =newLocation;
CLLocationCoordinate2D coordinate = [location coordinate];
latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSString *urlcvs=[[[NSString alloc]initWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json? latlng=%@,%@&sensor=false",latitude,longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urlss=[[NSURL alloc]initWithString:urlcvs];
NSURLRequest *req=[NSURLRequest requestWithURL:urlss];
NSData *res=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSString *mainstr=[[NSString alloc]initWithData:res encoding:NSUTF8StringEncoding];
NSDictionary *dic=[mainstr JSONValue];
NSString *tempstr=[[[dic valueForKey:@"results"]valueForKey:@"formatted_address"]objectAtIndex:0];
NSArray *array1=[tempstr componentsSeparatedByString:@","];
Citynamestr=[array1 objectAtIndex:array1.count-3];
NSLog(@"location is : %@",app.Citynamestr);
}