在Google自动完成列表中添加收藏的位置 [英] Adding favorite location in google autocomplete list

查看:95
本文介绍了在Google自动完成列表中添加收藏的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以向Google自动完成功能添加其他位置.

Is there any way to add an additional location to the Google autocomplete.

我需要在收藏夹位置"列表中进行搜索,并将该列表中的位置带到搜索结果的顶部.

I need to search in my 'favorite locations' list and bring the location from that list to the top of the search results.

请找到我用于Google自动填充的代码段.

Pls find the code snippet that I have used for google autocomplete.

let autocomplete = new google.maps.places.Autocomplete(elementRef, {
    componentRestrictions: { country: 'KW' }
});

我正在研究角度4.有人可以引导我吗?

I'm working on angular 4. Can anyone guid me ?

谢谢!

推荐答案

您可以使用 AutocompleteService类,并建立自己的建议列表.

You can use the AutocompleteService class and build your own suggestions list.

function initialize() {

    var search = 'Boston, MA';
    
    var service = new google.maps.places.AutocompleteService();
    service.getPlacePredictions({
        input: search,
        types: ['establishment', 'geocode']
    }, callback);
}

function callback(predictions, status) {

    if (status != google.maps.places.PlacesServiceStatus.OK) {
        alert(status);
        return;
    }

    var results = document.getElementById('results');

    for (var i = 0, prediction; prediction = predictions[i]; i++) {
        results.innerHTML += '<li><div class="result"><h3>' + prediction.description + '</h3>' + JSON.stringify(prediction) + '</div></li>';
    }
}

.result {
 padding: 10px;
 border: 1px solid black;
 margin-bottom: 10px;
}

<p>Query suggestions:</p>
<ul id="results"></ul>

<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initialize&key= AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"
        async defer></script>

然后由您决定如何根据需要设置样式并打印列表中所需的内容.如您所见,在callback函数中将收藏夹推到顶部很容易.

Then it's up to you to style it to your needs and to print what you want in your list. As you can see, it would be easy to push your favorites on top in the callback function.

这篇关于在Google自动完成列表中添加收藏的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆