如何在输入地址的同时发送新的地理定位请求并刷新谷歌地图

How to send new geolocalization request and refresh the Google map while typing address on input?

本文关键字:定位 请求 谷歌地图 刷新 输入 输入地 地址      更新时间:2023-09-26

我想在不刷新页面的情况下刷新谷歌地图并发送新的地理定位请求,同时用户在输入时键入地址(城市和地址,国家是恒定的)。我该怎么做?

<input type="text" id="input_adress" />
<script type="text/javascript">
function mapk() {
//code about start the map (new google.maps.Map)...
geokoder.geocode({address: 'UK'}, start_geocode);
}
function start_geocode(var1, status) {    
//more code...    
</script>

现在是我想要开始的代码:

$(function()
   {$('#input_adress').keyup(function() {
       });
   });

我不知道下一步该怎么办?也许我需要先选择地图。。。

geokoder.geocode({address: 'UK '+$('#input_adress').val()}, start_geocode);

我的页面上有jQuery库。这够了吗?

试试这样的东西:

$('#clickme').click(function() {
    geocoder.geocode({'address': $('#input_address').val()}, function(results, status) {
        setMap(results[0].geometry.location.lat(), results[0].geometry.location.lng());
    })
});
function setMap(lat, lng) {
    map_location = new google.maps.LatLng(lat, lng);
    marker.setPosition(map_location);
    map.setCenter(map_location);
}

这使用按钮上的click进行搜索,但您可以在输入上使用按键/更改。

此处的工作示例

通常,对于实时搜索/查询,我所做的是监视用户输入的按键,当出现暂停指示用户完成键入时,我将处理用户输入。

onkeydown:

clearTimeout(timer); timer = setTimeout(function(){ process(); }, 500);