Google Maps Places API V3自动完成功能在输入错误时选择第一个选项

时间:2019-07-05 16:28:24

标签: api google-maps autocomplete

我试图获取要在搜索中选择的第一项,但是当我键入内容并单击Enter时,我会得到一个错误。

我对使用新的google.maps.searchbar感到厌倦,但仍然无法反映出来。

function initMap() {
    @if(Request::path()=='owner/add-property')
    dragMap();
    @endif

    var input = document.getElementById('pac-input');
    var autocomplete = new google.maps.places.Autocomplete(input);
    var firstResult = $(".pac-container .pac-item:first").text();

    $("input").focusin(function () {
        $(document).keypress(function (e) {
            if (e.which == 13) {
                //alert("enter"); return false;
                //var places1 = new google.maps.places.Autocomplete(document.getElementById('pac-input'));
                var places2 = new google.maps.places.SearchBox(document.getElementById('pac-input'));


                    var place = places.getPlace();
                    var address = place.formatted_address;
                    var latitude = place.geometry.location.lat();
                    var longitude = place.geometry.location.lng();
                    var mesg = "Address: " + address;
                    mesg += "\nLatitude: " + latitude;
                    mesg += "\nLongitude: " + longitude;
                    //alert(mesg);
                    var html_data = '<input type="hidden" name="lat" value="'+latitude+'"/>';
                    html_data += '<input type="hidden" name="lng" value="'+longitude+'"/>';
                    html_data += '<input type="hidden" name="formatted_address" value="'+address+'"/>';
                    $("#set_location").html(html_data);
                    $("#search_location").html(html_data);


            }
        });
    });
    google.maps.event.addDomListener(window, 'load', function () {
        var places = new google.maps.places.Autocomplete(document.getElementById('pac-input'));

        google.maps.event.addListener(places, 'place_changed', function () {
            var place = places.getPlace();
            var address = place.formatted_address;
            var latitude = place.geometry.location.lat();
            var longitude = place.geometry.location.lng();
            var mesg = "Address: " + address;
            mesg += "\nLatitude: " + latitude;
            mesg += "\nLongitude: " + longitude;
            //alert(mesg);
            var html_data = '<input type="hidden" name="lat" value="'+latitude+'"/>';
            html_data += '<input type="hidden" name="lng" value="'+longitude+'"/>';
            html_data += '<input type="hidden" name="formatted_address" value="'+address+'"/>';
            $("#set_location").html(html_data);
            $("#search_location").html(html_data);


        });
    });
    @if(Request::path()=='short-term' || Request::path()=='search-property')
    initMaps();
    @endif 

    @if(Request::path()=='contact')
    contact_map();
    @endif ,

我收到以下错误。未定义索引:formatted_address

0 个答案:

没有答案