2014-01-05 34 views
1

如果我在viewDidLoad上调用方法,但它不会返回任何数据。如果我将该方法添加到按钮,并等待直到绘制位置,然后缩放它,则会正确拉取数据。我最初的想法是,一旦用户允许定位,它将提取数据,而不必等待点击按钮来调用该方法。只有在提取用户位置后才提取JSON数据

下面是代码:

- (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); 
} 

回答

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是伊娃返回一个有效的值。如果你想消除双重否定,你必须明确地初始化它是。

+0

我想根除双重否定。 –

+0

而且,在条件中使用它之前,您应该将它初始化为一个理智的默认值。 –

+0

好吧,所以我只是基本上添加布尔thisIsNotTheFirstTime;在我的伊娃头,默认情况下布尔是否? – user3117785