0

I am fairly new to iOS. I am trying to use google autocomplete and have it working...mostly. :)

I have a UISearchBar and when a user enters a string in search bar it autocompletes with predicted places (businesses). However when I click on a space the code suddenly goes to following method:- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

I need a way to search a string with spaces. For example is a user types in 'london' it is fine. As soon as the put 'london england' the program will go to the error method and fail. Also you technically wouldn't be able to write 'england' the error happens as soon as there is a space after london.

This is the page, that provides information but I can't see what I am after: https://developers.google.com/places/documentation/autocomplete#location_biasing

Below are the methods that I think are most relevant.

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

}
4

1 に答える 1

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]];
于 2013-09-10T20:14:28.523 に答える