【问题标题】:How to add extra result with google map api autocomplete?如何使用 google map api 自动完成添加额外的结果?
【发布时间】:2017-02-28 01:18:21
【问题描述】:

通常,如果我们使用 Google 地图自动完成,它会返回 5 行。例如,如果我们输入 dfw,谷歌自动完成将显示以下输出:

如何使用此 Google 地图自动完成结果添加额外结果?我想在结果中添加额外的行。预期输出如下:

我正在使用谷歌地图 JavaScript API。 var placeSearch,自动完成; var componentForm = { street_number: 'short_name', 路线:'long_name', 地点:'long_name', Administrative_area_level_1: 'short_name', 国家:'long_name', 邮政编码:'short_name' };

function initAutocomplete() {
    // Create the autocomplete object, restricting the search to geographical
    // location types.
    autocomplete = new google.maps.places.Autocomplete(
        (document.getElementById('autocomplete')),
        {types: ['geocode']});

    // When the user selects an address from the dropdown, populate the address
    // fields in the form.
    autocomplete.addListener('place_changed', fillInAddress);
}

function fillInAddress() {
    // Get the place details from the autocomplete object.
    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;
        }
    }
}

// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var geolocation = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };
            var circle = new google.maps.Circle({
                center: geolocation,
                radius: position.coords.accuracy
            });
            autocomplete.setBounds(circle.getBounds());
        });
    }
}

HTML 是:

<input id="street_number" name="street_number" type="hidden" />
<input id="route" name="route" type="hidden" />
<input id="locality" name="locality" type="hidden" />
<input id="administrative_area_level_1" name="administrative_area_level_1" type="hidden" />
<input id="country" name="country" type="hidden" />
<input id="postal_code" name="postal_code" type="hidden" />
<input id="autocomplete" type="text" placeholder="Enter your address" onFocus="geolocate()" />

【问题讨论】:

  • 我是否理解您希望在结果中包含机场的说法?还是您想包含自定义数据?
  • 我都想要。我可以包括机场或自定义地址(我的数据库中保存的家庭、办公室地址)。

标签: javascript google-maps-api-3 google-places-api


【解决方案1】:

我们这个..

var autocomplete = new google.maps.places.Autocomplete(document.getElementById('MyAutocomplete'));
        setTimeout(function(){
                            $(".pac-container").append('<div id="areasearch" class="pac-item" onmousedown="alert(\'Yes, working\')"><span class="pac-icon icon-airport"></span><span class="pac-item-query"><span class="pac-matched"></span>Sample Address</span> <span>it\'s Working</span></div>');
                        }, 500);

【讨论】:

  • 不要使用这个。无证且丑陋。
  • @Rashedul Alam 你最后有没有办法增加这个价值?它完成了工作,但将其添加到第一个索引,我想在最后一个索引添加自定义值?
【解决方案2】:

Google 为其地点搜索提供了多种地点类型。在这里你可以找到所有可用的Place Types,这里还有一些关于如何add places的信息。

【讨论】:

  • 感谢您的参考。但我不想在 Google 地图中添加新地点。我的位置已添加。我只想在谷歌自动完成结果中显示另一行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多