Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 如何避免最终用户手动启动GooglePlaces自动完成下拉列表?_Google Maps_Autocomplete_Triggers - Fatal编程技术网

Google maps 如何避免最终用户手动启动GooglePlaces自动完成下拉列表?

Google maps 如何避免最终用户手动启动GooglePlaces自动完成下拉列表?,google-maps,autocomplete,triggers,Google Maps,Autocomplete,Triggers,我正试图让google places autocomplete处理预定义的数据,但到目前为止运气不佳 假设我在一个预定义的输入字段中有一个文本“阿拉斯加南部,华盛顿州西雅图市”,如下所示: <input id="ev-loc-input" size="60" value="Alaskan Way South, Seattle, WA" /> 我不能使用这个,因为它会在地址查找完成后触发。我找不到可用的活动列表-也许有人知道 非常感谢任何帮助 我终于明白了!我已经做了两个小时了 无论

我正试图让google places autocomplete处理预定义的数据,但到目前为止运气不佳

假设我在一个预定义的输入字段中有一个文本“阿拉斯加南部,华盛顿州西雅图市”,如下所示:

<input id="ev-loc-input" size="60" value="Alaskan Way South, Seattle, WA" />
我不能使用这个,因为它会在地址查找完成后触发。我找不到可用的活动列表-也许有人知道


非常感谢任何帮助

我终于明白了!我已经做了两个小时了

无论如何,您需要关注您的输入元素,但您不能立即这样做。你需要推迟

所以,像这样

setTimeout(func, 2000);
function func() {
    input.focus();
    selectFirstResult();
}
这是我的代码,它是有效的

$(function() {
    var input = document.getElementById('searchTextField');
    input.value = "{{ $user["location"] }}";//I'm using laravel
    //You won't need the above line if you're already filling in the value of
    //the input field - I'm going to get this working without the map as well
    //I'll come back and post that later this weekend   
    var lat = -33.8688,
    lng = 151.2195,
    latlng = new google.maps.LatLng(lat, lng),
    image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png';     
    var mapOptions = {           
            center: new google.maps.LatLng(lat, lng),           
            zoom: 13,           
            mapTypeId: google.maps.MapTypeId.ROADMAP         
        },
        map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions),
        marker = new google.maps.Marker({
            position: latlng,
            map: map,
            icon: image
         });
    var autocomplete = new google.maps.places.Autocomplete(input, {
        types: ["geocode"]
    });          
    autocomplete.bindTo('bounds', map); 
    var infowindow = new google.maps.InfoWindow(); 
    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        infowindow.close();
        var place = autocomplete.getPlace();
        if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);  
        }
        moveMarker(place.name, place.geometry.location);
    });  
    $("input").focusin(function () {
        $(document).keypress(function (e) {
            if (e.which == 13) {
                 selectFirstResult();   
            }
        });
    });
    $("input").focusout(function () {
        if(!$(".pac-container").is(":focus") && !$(".pac-container").is(":visible"))
            selectFirstResult();
    });
     function selectFirstResult() {
        infowindow.close();
        $(".pac-container").hide();
        var firstResult = $(".pac-container .pac-item:first").text();
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({"address":firstResult }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var lat = results[0].geometry.location.lat(),
                lng = results[0].geometry.location.lng(),
                placeName = results[0].address_components[0].long_name,
                latlng = new google.maps.LatLng(lat, lng);
                moveMarker(placeName, latlng);
                $("input").val(firstResult);
            }
        });   
     }
     function moveMarker(placeName, latlng){
        marker.setIcon(image);
        marker.setPosition(latlng);
        infowindow.setContent(placeName);
        infowindow.open(map, marker);
     }
    setTimeout(func, 2000);
    function func() {
        input.focus();
        selectFirstResult();
    }
});

使用这段代码会让我产生强烈的自责感。一定有更好的办法。悔恨的感觉?代码没有“错误”。从这篇帖子开始,我实际上已经深入研究了这一点,从whta我可以看出这确实是唯一的方法。我认为这与谷歌使用的延迟加载有关。他们会这样做,这样你的网站就不会坐等谷歌的回应了。所以,这很烦人,但我认为这实际上是一件好事的结果。@AlexandreBourlier您链接到的更干净的解决方案解决了另一个问题。
$(function() {
    var input = document.getElementById('searchTextField');
    input.value = "{{ $user["location"] }}";//I'm using laravel
    //You won't need the above line if you're already filling in the value of
    //the input field - I'm going to get this working without the map as well
    //I'll come back and post that later this weekend   
    var lat = -33.8688,
    lng = 151.2195,
    latlng = new google.maps.LatLng(lat, lng),
    image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png';     
    var mapOptions = {           
            center: new google.maps.LatLng(lat, lng),           
            zoom: 13,           
            mapTypeId: google.maps.MapTypeId.ROADMAP         
        },
        map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions),
        marker = new google.maps.Marker({
            position: latlng,
            map: map,
            icon: image
         });
    var autocomplete = new google.maps.places.Autocomplete(input, {
        types: ["geocode"]
    });          
    autocomplete.bindTo('bounds', map); 
    var infowindow = new google.maps.InfoWindow(); 
    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        infowindow.close();
        var place = autocomplete.getPlace();
        if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);  
        }
        moveMarker(place.name, place.geometry.location);
    });  
    $("input").focusin(function () {
        $(document).keypress(function (e) {
            if (e.which == 13) {
                 selectFirstResult();   
            }
        });
    });
    $("input").focusout(function () {
        if(!$(".pac-container").is(":focus") && !$(".pac-container").is(":visible"))
            selectFirstResult();
    });
     function selectFirstResult() {
        infowindow.close();
        $(".pac-container").hide();
        var firstResult = $(".pac-container .pac-item:first").text();
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({"address":firstResult }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var lat = results[0].geometry.location.lat(),
                lng = results[0].geometry.location.lng(),
                placeName = results[0].address_components[0].long_name,
                latlng = new google.maps.LatLng(lat, lng);
                moveMarker(placeName, latlng);
                $("input").val(firstResult);
            }
        });   
     }
     function moveMarker(placeName, latlng){
        marker.setIcon(image);
        marker.setPosition(latlng);
        infowindow.setContent(placeName);
        infowindow.open(map, marker);
     }
    setTimeout(func, 2000);
    function func() {
        input.focus();
        selectFirstResult();
    }
});