【问题标题】:Adding favorite location in google autocomplete list在谷歌自动完成列表中添加最喜欢的位置
【发布时间】:2017-10-31 10:50:05
【问题描述】:

有没有办法向 Google 自动填充添加额外的位置。

我需要在我的“最喜欢的位置”列表中进行搜索,并将该列表中的位置带到搜索结果的顶部。

请找到我用于 google 自动完成的代码 sn-p。

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

我正在研究 Angular 4。有人可以指导我吗?

谢谢!

【问题讨论】:

    标签: angular google-maps-api-3 google-location-services


    【解决方案1】:

    您可以使用AutocompleteService class 并建立自己的建议列表。

    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 函数中将您的收藏夹放在首位会很容易。

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 2017-09-05
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 2014-10-13
      相关资源
      最近更新 更多