gwa*_*ton 3 html javascript forms google-location-services
编辑2 - 因为我没有得到第一个赏金的解决方案,我愿意给任何能够成功解决这个问题的人给予100分:)
编辑:请阅读问题!我不是问如何DO Google地方查找,所以请不要只是张贴,作为一个答案!
我使用Google Places API在地理位置表单中添加了标准字段.
<div><input class="input_standard fakeWaitlistCity" placeholder="Location" id="autocomplete"></input></div>
Run Code Online (Sandbox Code Playgroud)
在用户输入时,它会显示以下建议.是否有设置也用第一个返回值填写字段?
这是它目前的样子:
这里的目标是用户不能只键入"纽约".如果他们停止打字,整个领域将已经填满"纽约,纽约,美国".
这是用户输入的理想体验:
谢谢!
更新 - 我发现了关于它的名称的讨论.
以下是我正在谈论的上述链接中的图片:
// Bind dollar signs to query selector (IE8+)
var $ = document.querySelector.bind(document);
function preventStandardForm(evt) {
// prevent standard form from submitting
evt.preventDefault();
}
function autoCallback(predictions, status) {
// *Callback from async google places call
if (status != google.maps.places.PlacesServiceStatus.OK) {
// show that this address is an error
pacInput.className = 'error';
return;
}
// Show a successfull return
pacInput.className = 'success';
pacInput.value = predictions[0].description;
}
function queryAutocomplete(input) {
// *Uses Google's autocomplete service to select an address
var service = new google.maps.places.AutocompleteService();
service.getPlacePredictions({
input: input,
componentRestrictions: {
country: 'us'
}
}, autoCallback);
}
function handleTabbingOnInput(evt) {
// *Handles Tab event on delivery-location input
if (evt.target.id == "pac-input") {
// Remove active class
evt.target.className = '';
// Check if a tab was pressed
if (evt.which == 9 || evt.keyCode == 9) {
queryAutocomplete(evt.target.value);
}
}
}
// ***** Initializations ***** //
// initialize pac search field //
var pacInput = $('#pac-input');
pacInput.focus();
// Initialize Autocomplete
var options = {
componentRestrictions: {
country: 'us'
}
};
var autocomplete = new google.maps.places.Autocomplete(pacInput, options);
// ***** End Initializations ***** //
// ***** Event Listeners ***** //
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var result = autocomplete.getPlace();
if (typeof result.address_components == 'undefined') {
queryAutocomplete(result.name);
} else {
console.log(result.address_components);
}
});
// Tabbing Event Listener
if (document.addEventListener) {
document.addEventListener('keydown', handleTabbingOnInput, false);
} else if (document.attachEvent) { // IE8 and below
document.attachEvent("onsubmit", handleTabbingOnInput);
}
// search form listener
var standardForm = $('#search-shop-form');
if (standardForm.addEventListener) {
standardForm.addEventListener("submit", preventStandardForm, false);
} else if (standardForm.attachEvent) { // IE8 and below
standardForm.attachEvent("onsubmit", preventStandardForm);
}
// ***** End Event Listeners ***** //Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="${createLinkTo(dir:'asset/vendor/bootstrap/dist/css',file:'bootstrap.css')}">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places&key=AIzaSyBI9iSbSEo3h0LdqlqRFwnayYApQfmNXuE"></script>
<form id="search-shop-form" class="search-form" name="searchShopForm" action="/impl_custom/index/search/" method="post">
<label for="pac-input">Delivery Location</label>
<input id="pac-input" type="text" placeholder="Los Angeles, Manhattan, Houston" autocomplete="off" />
<button class="search-btn btn-success" type="submit">Search</button>
</form>Run Code Online (Sandbox Code Playgroud)
更新:
注意:根据您的要求,Google地方可以选择任何选项.因此,您可以根据需要更改超时秒数.我假设300ms我会得到建议下拉.
$("#find").click(function(){
$("#geocomplete").trigger("geocode");
});
$(function(){
$("#geocomplete").geocomplete()
});
$('.autoc').bind('keyup', function(e) {
if(e.keyCode==13){
$('#find').click()
}else{
setTimeout(function(){
var inputVal = $("#geocomplete").val();
var inpt = $("#geocomplete");
if( inputVal != "" ){
var firstRowVal = firstRowValue();
if( firstRowVal != "" ){
if(firstRowVal.toLowerCase().indexOf(inputVal.toLowerCase()) === 0){
inpt.val(firstRowVal);//change the input to the first match
inpt[0].selectionStart = inputVal.length; //highlight from end of input
inpt[0].selectionEnd = firstRowVal.length;//highlight to the end
}
}
}
}, 300);// please change this 300 as per your internet connection speed.
}
});
function firstRowValue() {
var selected = '';
// Check if any result is selected.
if ($(".pac-item-selected")[0]) {
selected = '-selected';
}
// Get the first suggestion's text.
var $span1 = $(".pac-container:visible .pac-item" + selected + ":first span:nth-child(2)").text();
var $span2 = $(".pac-container:visible .pac-item" + selected + ":first span:nth-child(3)").text();
// Adds the additional information, if available.
var firstResult = $span1;
if ($span2) {
firstResult += " - " + $span2;
}
return firstResult;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyBI9iSbSEo3h0LdqlqRFwnayYApQfmNXuE"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://ubilabs.github.io/geocomplete/jquery.geocomplete.js"></script>
<form>
<input id="geocomplete" type="text" placeholder="Type in an address" size="50" class="autoc"/>
<input id="find" type="button" value="find" size="10"/>
</form>Run Code Online (Sandbox Code Playgroud)
希望这会对你有所帮助
这里参考