有一个问题,我在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);
}
答案 0 :(得分:1)
我猜测currentCentre
的值在viewDidLoad时间是零点因此缺乏食物&餐厅。
您将不得不等待您的用户位置通过挂钩
返回有效值 - (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
是伊娃。如果你想根除双重否定,你必须明确地启动它。