2017-10-31 10 views
0

Googleオートコンプリートに追加の場所を追加する方法はありますか? screenshotGoogleオートコンプリートリストにお気に入りの場所を追加する

「お気に入りの場所」リストで検索し、そのリストから検索結果の先頭に移動する必要があります。

私はGoogleオートコンプリートに使用したコードスニペットを見つけます。

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

私は角4に取り組んでいます。

ありがとうございます!

答えて

0

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" 
 
     async defer></script>

それからそれはあなたのニーズに合わせてスタイルを、あなたのリストに欲しいものを印刷するのはあなた次第です。ご覧のとおり、callback機能でお気に入りをプッシュするのは簡単です。

関連する問題