Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use the Autocomplete API with Google Maps SDK on iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.17k
    Comment on it

    By Using the following code you can integrate the Google Maps SDK in your iOS project.

    First we will write the function which results the array of related nearby places.

    -(void) searchLocation:(NSString *)locationName{
        GMSAutocompleteFilter *filter;
        filter.type=kGMSPlacesAutocompleteTypeFilterGeocode;
        GMSVisibleRegion visibleRegion = _mapView.projection.visibleRegion;
        GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion: visibleRegion];
        [[GMSPlacesClient new] autocompleteQuery:locationName bounds:bounds filter:filter callback:^(NSArray *results,NSError *error){
            if (error != nil) {
                [locationAray removeAllObjects];
                [self.locationTableView reloadData];
                return;
            }
            
            if ([results count]>0) {
                [locationAray removeAllObjects];
                [locationAray addObjectsFromArray:results];
                [self.locationTableView reloadData];
            }
            else{
                [locationAray removeAllObjects];
                [self.locationTableView reloadData];
            }
        }
         ];
    }
    

    After that we will use the delegate of UITableView and populate the data in tableview.

    # pragma mark tableView Delagate
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return  [locationAray count];
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *CellIdentifier = @"location";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        GMSAutocompletePrediction *alocation=[locationAray objectAtIndex:indexPath.row];
        [cell.lblText setText:alocation.attributedPrimaryText.string];
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [self.view endEditing:YES];
        GMSAutocompletePrediction *alocation = [locationAray objectAtIndex:indexPath.row];
        
        [[GMSPlacesClient new]lookUpPlaceID:alocation.placeID callback:^(GMSPlace * _Nullable result, NSError * _Nullable error) {
            if (error==nil) {
                // do your code after selecting the place
            }
        }];
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: