2014-09-12 42 views
2

我正在使用Google API的應用程序。我試圖通過它找到我周圍的地方。我使用下面的代碼獲取數據,如何使用谷歌API找到我周圍的地方

-(void)fetchedData:(NSData *)responseData { 
    //this is to parse out the json data 
    NSError *error = nil; //create some error handling here 
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

    //I'm told the returned results from Google will be an array obtained from the NSDictionary object with the key "results" 

    NSArray *places = [json objectForKey:@"results"]; 

    //display the data to the console for review 
    NSLog(@" Google data:\n%@", places); 

} 

但它顯示json status = Request Denied。 任何幫助將不勝感激。

回答

2

維沙爾,

則可以使用代碼以下塊做:

- (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=%@&language=%@", appDelegate.currentLatitude, appDelegate.currentLongitude, [radiusValueArray objectAtIndex:[[NSUserDefaults standardUserDefaults] integerForKey:@"selectedDistance"]], googleType, kGOOGLE_API_KEY, appDelegate.selectedLanguageCode]; 

    //Formulate the string as URL object. 
    NSURL *googleRequestURL=[NSURL URLWithString:url]; 

    // Retrieve the results of the URL. 
    dispatch_async(kBgQueue, ^{ 
     NSData* data = [NSData dataWithContentsOfURL: googleRequestURL]; 
     if(data == nil) 
     { 
      [placeTableView reloadData]; 
      [SVProgressHUD dismiss]; 
     } 
     else 
     { 
      [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; 
     } 
    }); 
} 

- (void) queryGooglePlaces_WithNextPage 
{ 
    // Build the url string we are going to sent to Google. NOTE: The kGOOGLE_API_KEY is a constant which should contain your own API key that you can obtain from Google. See this link for more info: 

    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?pagetoken=%@&location=%f,%f&radius=%@&sensor=true&key=%@", nextPageToken, appDelegate.currentLatitude, appDelegate.currentLongitude, [radiusValueArray objectAtIndex:[[NSUserDefaults standardUserDefaults] integerForKey:@"selectedDistance"]], kGOOGLE_API_KEY]; 

    //Formulate the string as URL object. 
    NSURL *googleRequestURL=[NSURL URLWithString:url]; 

    // Retrieve the results of the URL. 
    dispatch_async(kBgQueue, ^{ 
     NSData* data = [NSData dataWithContentsOfURL: googleRequestURL]; 
     if(data == nil) 
     { 
      [placeTableView reloadData]; 
      [SVProgressHUD dismiss]; 
     } 
     else 
     { 
      [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". 

    if(isNextPageAvailable == FALSE) 
     [appDelegate.placesArray removeAllObjects]; 

    NSArray *placesTemp = [json objectForKey:@"results"]; 

    if([json valueForKey:@"next_page_token"] != nil) 
    { 
     nextPageToken = [json valueForKey:@"next_page_token"]; 
     isNextPageAvailable = TRUE; 
    } 
    else 
    { 
     nextPageToken = @""; 
     isNextPageAvailable = FALSE; 
    } 


    for(int i=0;i<[placesTemp count];i++) 
    { 
     NSMutableDictionary *placeDictionary = [[NSMutableDictionary alloc] initWithDictionary:[placesTemp objectAtIndex:i]]; 

     double lat1 = appDelegate.currentLatitude; 
     double long1 = appDelegate.currentLongitude; 
     double lat2 = [[[[placeDictionary objectForKey:@"geometry"] objectForKey:@"location"] valueForKey:@"lat"] doubleValue]; 
     double long2 = [[[[placeDictionary objectForKey:@"geometry"] objectForKey:@"location"] valueForKey:@"lng"] doubleValue]; 

     CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1]; 
     CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2]; 

     [placeDictionary setValue:[NSString stringWithFormat:@"%f",[location1 distanceFromLocation:location2]] forKey:@"distance"]; 

     [appDelegate.placesArray addObject:placeDictionary]; 
    } 

    NSSortDescriptor *sortDescriptor; 
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { 
     if ([obj1 floatValue] < [obj2 floatValue]) 
      return NSOrderedAscending; 
     else 
      return NSOrderedDescending; 
    }]; 

    NSArray *sortedArray = [appDelegate.placesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
    [appDelegate.placesArray removeAllObjects]; 
    [appDelegate.placesArray addObjectsFromArray:sortedArray]; 

    [self showPoweredbyGoogle]; 

    [placeTableView reloadData]; 
    [SVProgressHUD dismiss]; 

// [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(reloadTableNow) userInfo:nil repeats:NO]; 

    //Plot the data in the places array onto the map with the plotPostions method. 
    // [self plotPositions:placesArray]; 
} 

- (void) queryGooglePlaceDetail 
{ 
    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/details/json?reference=%@&sensor=false&key=%@&language=%@", [[appDelegate.placesArray objectAtIndex:selectedPlaceIndex] valueForKey:@"reference"], kGOOGLE_API_KEY, appDelegate.selectedLanguageCode]; 

    //Formulate the string as URL object. 
    NSURL *googleRequestURL=[NSURL URLWithString:url]; 

    // Retrieve the results of the URL. 
    dispatch_async(kBgQueue, ^{ 
     NSData* data = [NSData dataWithContentsOfURL: googleRequestURL]; 

     if(data == nil) 
     { 
      [SVProgressHUD dismiss]; 
     } 
     else 
     { 
      [self performSelectorOnMainThread:@selector(fetchedDetailPlaceData:) withObject:data waitUntilDone:YES]; 
     } 
    }); 
} 

- (void)fetchedDetailPlaceData:(NSData *)responseData 
{ 
    //parse out the json data 
    NSError* error; 
    NSDictionary *json = [NSJSONSerialization 
          JSONObjectWithData:responseData 

          options:kNilOptions 
          error:&error]; 

    NSDictionary *detailTempDic = [[NSMutableDictionary alloc] initWithDictionary:[json objectForKey:@"result"]]; 
    [detailTempDic setValue:[[appDelegate.placesArray objectAtIndex:selectedPlaceIndex] valueForKey:@"distance"] forKey:@"distance"]; 

    [SVProgressHUD dismiss]; 

    [self performSegueWithIdentifier:@"Detail_Place_Push" sender:detailTempDic]; 
} 

在這裏,你必須通過不同類型的谷歌的地方的對象,如ATM,機場,餐廳,銀行,醫院,學校。

  [self queryGooglePlaces:[googlePlaceTypeArray objectAtIndex:SharedManager.selectedPlaceCategoryIndex]]; 

感謝, 最好的問候, Gurprit