如何使用谷歌地图api自动完成添加额外的结果?

时间:2016-10-18 16:45:23

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

通常,如果我们使用Google地图自动填充功能,则会返回5行。例如,如果我们输入 dfw ,Google自动填充功能将显示以下输出: enter image description here

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

enter image description here

我正在使用谷歌地图JavaScript API。     var placeSearch,autocomplete;     var componentForm = {         street_number:'short_name',         路线:'long_name',         locality:'long_name',         administrative_area_level_1:'short_name',         国家:'long_name',         postal_code:'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()" />

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我们这个..

SSE:AES256