(iOS)如何在搜索栏的Google自动填充功能上添加空间

时间:2013-09-10 19:51:08

标签: ios json autocomplete uisearchbar

我对iOS很新。我正在尝试使用谷歌自动完成功能并让它正常工作...... :)

我有一个UISearchBar,当用户在搜索栏中输入一个字符串时,它会自动填充预测的地点(商家)。但是,当我单击一个空格时,代码突然转到以下方法: - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

我需要一种用空格搜索字符串的方法。例如,用户输入'london'就可以了。一旦放入'london england',程序将转到错误方法并失败。你在技术上也不能写“英格兰”,一旦伦敦之后有空格就会发生错误。

这是提供信息的页面,但我无法看到我的信息:https://developers.google.com/places/documentation/autocomplete#location_biasing

以下是我认为最相关的方法。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.autoCompleteResults = [NSJSONSerialization JSONObjectWithData:self.autoCompleteNSData options:nil error:nil];
    //test logging
    NSLog(@"Connection did finish loading");
    NSLog(@"Auto Complete Results - Dictionary: %@ ", self.autoCompleteResults);
    //parse the auto complete results
    self.filteredStrings = [self.autoCompleteResults objectForKey:@"predictions" ];
    NSLog(@"Auto Complete Results - Array: %@", self.filteredStrings);
    int i = [self.filteredStrings count];
    NSLog(@"Count of array: %i", i);
    int n = 0;
    for (n = 0; n < i;n= n+1) {
        NSString *tempDescription = [[self.filteredStrings objectAtIndex:n]objectForKey:@"description"];
        NSLog(@"tempDescription: %@", tempDescription);

    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection didfail with error");
    UIAlertView *connectionErrorView = [[UIAlertView alloc]initWithTitle:@"Connection error" message:@"There is a problem with the connection. Please ensure that you are connected to 3G or Wi-Fi" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [connectionErrorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}


//self.filteredStrings is a NSMutableArray (not dictionary)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (self.filteredStrings) {
        cell.textLabel.text = [[self.filteredStrings objectAtIndex:indexPath.row]objectForKey:@"description"];

    }else{
        cell.textLabel.text = @"Currently no results";
    }

        return cell;
    }
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //To check if alphanumerical characters have been entered
    NSString *alphaStr = @"aA09";
    NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
    BOOL valid = [[alphaStr stringByTrimmingCharactersInSet:alphaSet] isEqualToString:searchText];

    NSLog(@"SearchText String: %@", searchText);

    //The if is needed because without it the program crashes when you clear the field via the 'X' button in search bar
    if (valid) {
        NSLog(@"SearchText passed !SearchText: %@", searchText);
    }else{
        NSLog(@"SearchText should be full: %@", searchText);
    NSString *autoCompleteUrl = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=establishment&location=37.76999,-122.44696&radius=500&sensor=true&key=Mykey", searchText];
    NSURL *url = [NSURL URLWithString:autoCompleteUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc]initWithRequest:request delegate:self];

    [self.autoCompleteTableView reloadData];
    }

}

1 个答案:

答案 0 :(得分:0)

NSString *autoCompleteUrl = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=establishment&location=37.76999,-122.44696&radius=500&sensor=true&key=Mykey", [searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];