Javascript 让您按(地区)或(城市)或任何类型进行筛选。我对此进行了测试,但它没有按预期工作。当您键入时,SearchBox会发出“自动完成”请求,但当您按enter键时,它会使用您输入的字符串在处发出请求。这可能返回也可能不返回实际结果。例如,通过键入“naxo”,
Javascript 让您按(地区)或(城市)或任何类型进行筛选。我对此进行了测试,但它没有按预期工作。当您键入时,SearchBox会发出“自动完成”请求,但当您按enter键时,它会使用您输入的字符串在处发出请求。这可能返回也可能不返回实际结果。例如,通过键入“naxo”,,javascript,google-maps,google-maps-api-3,google-places-api,google-places,Javascript,Google Maps,Google Maps Api 3,Google Places Api,Google Places,让您按(地区)或(城市)或任何类型进行筛选。我对此进行了测试,但它没有按预期工作。当您键入时,SearchBox会发出“自动完成”请求,但当您按enter键时,它会使用您输入的字符串在处发出请求。这可能返回也可能不返回实际结果。例如,通过键入“naxo”,自动完成列表将作为第一个结果返回“Naxos ke Mikres Kyklades”(可能这是基于区域设置的?)。按enter键,不选择列表项,返回0个结果。搜索框非常方便。我真的希望它能让我把搜索结果限制在地理编码或地址上。但搜索框和自动完成
让您按(地区)或(城市)或任何类型进行筛选。我对此进行了测试,但它没有按预期工作。当您键入时,SearchBox会发出“自动完成”请求,但当您按enter键时,它会使用您输入的字符串在处发出请求。这可能返回也可能不返回实际结果。例如,通过键入“naxo”,自动完成列表将作为第一个结果返回“Naxos ke Mikres Kyklades”(可能这是基于区域设置的?)。按enter键,不选择列表项,返回0个结果。搜索框非常方便。我真的希望它能让我把搜索结果限制在地理编码或地址上。但搜索框和自动完成不同。更多的是像“纽约咖啡”这样的东西,我已经使用Autocomplete大约一年了,在阅读了这篇文章并切换到SearchBox之后,一切都好多了。我有一个特殊的“getPlaceFromAutocompleteList”函数作为解决方法,但它修复了几个令人沮丧的边缘案例错误。非常感谢。这在使用箭头键导航并按enter键选择的情况下不起作用。@dnlmzw如果使用箭头键导航并按enter键选择,则会触发“place_change”…@user151496这是真的,但如果您的字段是
字段则不起作用,那么您应该执行e.preventDefault()按enter(13/$.ui.keyCode.enter
)keydown
,如果不按,请求的响应可能不会及时返回:)经过数小时的搜索和尝试破解类似的解决方案,这对我来说非常有效。这就像一个魅力。它需要jQuery。如果你和我一样在Angular上,这里有一个关于如何安装它的小指南:还要确保在var autocomplete=new google.maps.places.autocomplete(输入)之前调用该函数代码>否则它将不起作用。答案很好,代码格式糟糕。无论如何谢谢:)@Derenir已修复!我修改了上面的函数,它对我很有用listner使kepdown为true并选择此.searchElementRef.nativeElement.addEventListener('keydown',(e)=>{if(e.keyCode==13 | | e.keyCode==9){var firstValue=$('.pac container.pac item:first')。text();//如果用户未使用箭头导航,且此操作尚未运行,则this.txtValue=firstValue;google.maps.event.trigger(e.target,'keydown',{keyCode:40,hasRanOnce:true,});});
$("input#autocomplete").keydown(function(e) {
if (e.which == 13) {
//if there are suggestions...
if ($(".pac-container .pac-item").length) {
//click on the first item in the list or simulate a down arrow key event
//it does get this far, but I can't find a way to actually select the item
$(".pac-container .pac-item:first").click();
} else {
//there are no suggestions
}
}
});
$("input#autocomplete").focusin(function () {
$(document).keypress(function (e) {
if (e.which == 13) {
$("input#autocomplete").trigger('focus');
$("input#autocomplete").simulate('keydown', { keyCode: $.ui.keyCode.DOWN } ).simulate('keydown', { keyCode: $.ui.keyCode.ENTER });
}
});
});
document.getElementById("adress").value = place.formatted_address;
var searchBox = new google.maps.places.SearchBox(document.getElementById('searchinput'));
google.maps.event.addListener(searchBox, 'places_changed', function() {
var place = searchBox.getPlaces()[0];
if (!place.geometry) return;
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(16);
}
});
<form method="get" ng-app="StarterApp" ng-controller="AppCtrl" action="searchresults.html" id="target" autocomplete="off">
<br/>
<div class="row">
<div class="col-md-4"><input class="form-control" tabindex="1" autofocus g-places-autocomplete force-selection="true" ng-model="user.fromPlace" placeholder="From Place" autocomplete="off" required>
</div>
<div class="col-md-4"><input class="form-control" tabindex="2" g-places-autocomplete force-selection="true" placeholder="To Place" autocomplete="off" ng-model="user.toPlace" required>
</div>
<div class="col-md-4"> <input class="btn btn-primary" type="submit" value="submit"></div></div><br /><br/>
<input class="form-control" style="width:40%" type="text" name="sourceAddressLat" placeholder="From Place Lat" id="fromLat">
<input class="form-control" style="width:40%"type="text" name="sourceAddressLang" placeholder="From Place Long" id="fromLong">
<input class="form-control" style="width:40%"type="text" name="sourceAddress" placeholder="From Place City" id="fromCity">
<input class="form-control" style="width:40%"type="text" name="destinationAddressLat" placeholder="To Place Lat" id="toLat">
<input class="form-control" style="width:40%"type="text" name="destinationAddressLang" placeholder="To Place Long"id="toLong">
<input class="form-control" style="width:40%"type="text" name="destinationAddress"placeholder="To Place City" id="toCity">
</form>
<input name="location" id="autocomplete" autocomplete="off" type="text" class="textbx" placeholder="Enter Destination" required>
function initialize() {
// Create the autocomplete object, restricting the search
// to geographical location types.
if($('#autocomplete').length){
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
{
types: ['(regions)'],
componentRestrictions: {country: "in"}
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
$('#autocomplete').closest('form').data('changed', true);
fillInAddress();
});
}
//select first result
$('#autocomplete').keydown(function (e) {
if (e.keyCode == 13 || e.keyCode == 9) {
$(e.target).blur();
if($(".pac-container .pac-item:first span:eq(3)").text() == "")
var firstResult = $(".pac-container .pac-item:first .pac-item-query").text();
else
var firstResult = $(".pac-container .pac-item:first .pac-item-query").text() + ", " + $(".pac-container .pac-item:first span:eq(3)").text();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({"address":firstResult }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
placeName = results[0];
e.target.value = firstResult;
fillInAddress(placeName);
$('#datetimepicker1 .input-group-addon').click();
}
});
}
});
}
// [START region_fillform]
function fillInAddress(place) {
// Get the place details from the autocomplete object.
if(!place)
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
var selectFirstOnEnter = function(input) { // store the original event binding function
var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;
function addEventListenerWrapper(type, listener) { // Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected, and then trigger the original listener.
if (type == "keydown") {
var orig_listener = listener;
listener = function(event) {
var suggestion_selected = $(".pac-item-selected").length > 0;
if (event.which == 13 && !suggestion_selected) {
var simulated_downarrow = $.Event("keydown", {keyCode: 40, which: 40});
orig_listener.apply(input, [simulated_downarrow]);
}
orig_listener.apply(input, [event]);
};
}
_addEventListener.apply(input, [type, listener]); // add the modified listener
}
if (input.addEventListener) {
input.addEventListener = addEventListenerWrapper;
} else if (input.attachEvent) {
input.attachEvent = addEventListenerWrapper;
}
}
selectFirstOnEnter(input1);
selectFirstOnEnter(input2);
...
// search input
const searchInput = document.getElementById('js-search-input');
// Google Maps autocomplete
const autocomplete = new google.maps.places.Autocomplete(searchInput);
// Has user pressed the down key to navigate autocomplete options?
let hasDownBeenPressed = false;
// Listener outside to stop nested loop returning odd results
searchInput.addEventListener('keydown', (e) => {
if (e.keyCode === 40) {
hasDownBeenPressed = true;
}
});
// GoogleMaps API custom eventlistener method
google.maps.event.addDomListener(searchInput, 'keydown', (e) => {
// Maps API e.stopPropagation();
e.cancelBubble = true;
// If enter key, or tab key
if (e.keyCode === 13 || e.keyCode === 9) {
// If user isn't navigating using arrows and this hasn't ran yet
if (!hasDownBeenPressed && !e.hasRanOnce) {
google.maps.event.trigger(e.target, 'keydown', {
keyCode: 40,
hasRanOnce: true,
});
}
}
});
// Clear the input on focus, reset hasDownBeenPressed
searchInput.addEventListener('focus', () => {
hasDownBeenPressed = false;
searchInput.value = '';
});
// place_changed GoogleMaps listener when we do submit
google.maps.event.addListener(autocomplete, 'place_changed', function() {
// Get the place info from the autocomplete Api
const place = autocomplete.getPlace();
//If we can find the place lets go to it
if (typeof place.address_components !== 'undefined') {
// reset hasDownBeenPressed in case they don't unfocus
hasDownBeenPressed = false;
}
});
autocomplete.addListener('place_changed', function() {
if(event.keyCode == 13 || event.keyCode == 9) { // detect the enter key
var firstValue = $(".pac-container .pac-item:first").text(); // assign to this variable the first string from the autocomplete dropdown
}
$('#search-address').val(firstValue); // add this string to input
console.log(firstValue); // display the string on your browser console to check what it is
//(...) add the rest of your code here
});
}