我在调用 viewDidLoad 上的方法时出现问题,但它不会返回任何数据。如果我将方法添加到按钮,并等到绘制位置并缩放它,数据就会被正确提取。我最初的想法是,一旦用户允许定位,它就会提取数据,而不必等待单击按钮来调用该方法。
代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
// Instantiating location object
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
// Setting some parameters for the location object
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// THIS IS THE METHOD NOT WORKING UNLESS ADDED TO A BUTTON, AND WAITING
// FOR IT TO ZOOM, WHICH I ASSUME READ THE USERS PLOTTED LOCATION?
[self queryGooglePlaces:@"food|restaurant"];
// Do any additional setup after loading the view.
}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKCoordinateRegion region;
region = MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate, 10*METERS_PER_MILE, 10*METERS_PER_MILE);
[mv setRegion:region animated:YES];
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//Get the east and west points on the map so you can calculate the distance (zoom level) of the current map view.
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
//Set your current distance instance variable.
currentDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
//Set your current center point on the map instance variable.
currentCentre = self.mapView.centerCoordinate;
}
-(void) queryGooglePlaces: (NSString *) googleType
{
NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=%@&sensor=true&key=%@", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:@"%i", currentDist], googleType, kGOOGLE_API_KEY];
NSURL *url2 = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
// Formulate the string as an URL object
NSURL *googleRequestURL = url2;
// Retrieve the results of the URL
dispatch_async(kBgQueue, ^{
NSData *data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:@selector(fetchedData:)withObject:data waitUntilDone:YES];
});
}
-(void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
//The results from Google will be an array obtained from the NSDictionary object with the key "results".
NSArray* places = [json objectForKey:@"results"];
//Write out the data to the console.
NSLog(@"Google Data: %@", places);
}
最佳答案
我猜测 currentCentre
的值在 viewDidLoad 时间是零点,因此缺少食物和餐厅。
你将不得不等待你的用户位置通过 Hook 返回一个有效值
- (void)mapView:(MKMapView *)amapView didUpdateUserLocation:(MKUserLocation *)userLocation
可能
- (void)mapView:(MKMapView *)amapView didUpdateUserLocation:(MKUserLocation *)userLocation {
currentCentre = userLocation.location.coordinate;
if(thisIsNotTheFirstTime == NO) {
[self queryGooglePlaces:@"food|restaurant"];
thisIsNotTheFirstTime = YES;
}
}
其中 thisIsNotTheFirstTime
是一个 ivar。如果你想消除双重否定,你必须明确地将它初始化为 YES。
关于ios - 仅在获取用户位置后才拉取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20939927/