bassjobsen / Bootstrap-3-Typeahead

The Typeahead plugin from Twitter's Bootstrap 2 ready to use with Bootstrap 3 and Bootstrap 4
1.68k stars 1.32k forks source link

Problem with data and typeahead #191

Open victorh1705 opened 8 years ago

victorh1705 commented 8 years ago

I would like to use Google Places API Web Service to find cities around the world using Bootstrap-3-Typeahead. But I'm stuck right here.

HTML

           <form name="google">

                <label for="cities">Cities</label>
                <input type="text" class="form-control"
                id="cities" placeholder="cities" data-provide="typeahead"/>

            </form>

JS


        var $input = $('#cities');
            $input.typeahead({
                source:function (query) {
                    var ajaxResponse;
                        $.get({
                            url: "https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+query+"&types=(cities)&language=pt_BR&key=mykey",
                            success : function (response) {console.log(response);
                                ajaxResponse = response;
                            },
                        });
                    return ajaxResponse;

                },

            },'json');

        $input.change(function() {
            var current = $input.typeahead("getActive");
            if (current) {
                // Some item from your model is active!
                if (current.name == $input.val()) {
                    // This means the exact match is found. Use toLowerCase() if you want case insensitive match.
                } else {
                    // This means it is only a partial match, you can either add a new item 
                    // or take the active if you don't want new items
                }
            } else {
                // Nothing is active so it is a new value (or maybe empty value)
            }
        });  
andreich1980 commented 8 years ago

Hello. Did you try this? source:function (query, process) { ...

success : function (response) {
                                console.log(response);
                                ajaxResponse = response;
                                return process(response);
                            },