Google maps google places API-仅限返回完整地址

Google maps google places API-仅限返回完整地址,google-maps,google-places-api,Google Maps,Google Places Api,有人知道一种限制GooglePlacesAPI只返回完整地址结果的方法吗?例如,地址的前两行。 我想删掉城镇,因为它们太宽了 我看到postcodeanywhere只提供完整的地址,但它们没有免费的层 谢谢。您已经在实例中选择了要从自动完成ie街号和路线返回的参数 基本用法在这里 记住以下几点;您的输入字段必须根据自动完成参数进行命名,才能开箱即用,如街道号码、路线、邮政编码等,否则API将不会写入表单中的这些字段 我包括的小提琴中的JS示例: var placeSearch; var comp

有人知道一种限制GooglePlacesAPI只返回完整地址结果的方法吗?例如,地址的前两行。 我想删掉城镇,因为它们太宽了

我看到postcodeanywhere只提供完整的地址,但它们没有免费的层


谢谢。

您已经在实例中选择了要从自动完成ie街号和路线返回的参数

基本用法在这里

记住以下几点;您的输入字段必须根据自动完成参数进行命名,才能开箱即用,如街道号码、路线、邮政编码等,否则API将不会写入表单中的这些字段

我包括的小提琴中的JS示例:

var placeSearch;
var componentForm = {
    street_number: 'long_name',
    route: 'long_name'
};

var mapOptions = {
    center: new google.maps.LatLng(41.877461, -87.638085),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    scrollwheel: false,
    disableDefaultUI: true,
    streetViewControl: false,
    panControl: false
};

var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);

var input = /** @type {HTMLInputElement} */
(
document.getElementById('field_autocomplete'));

var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);

var infowindow = new google.maps.InfoWindow();

var marker = new google.maps.Marker({
    map: map,
    anchorPoint: new google.maps.Point(0, -29)
});

google.maps.event.addListener(autocomplete, 'place_changed', function () {
    infowindow.close();
    marker.setVisible(false);

    var place = autocomplete.getPlace();

    if (!place.geometry) {
        window.alert("Autocomplete's returned place contains no geometry");
        return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
    } else {
        map.setCenter(place.geometry.location);
        map.setZoom(12); // Why 17? Because it looks good.
    }
    marker.setIcon( /** @type {google.maps.Icon} */ ({
        url: place.icon,
        size: new google.maps.Size(71, 71),
        origin: new google.maps.Point(0, 0),
        anchor: new google.maps.Point(25, 34),
        scaledSize: new google.maps.Size(50, 50)
    }));
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';
    if (place.address_components) {
        address = [
        (place.address_components[0] && place.address_components[0].short_name || ''), (place.address_components[1] && place.address_components[1].short_name || ''), (place.address_components[2] && place.address_components[2].short_name || '')].join(' ');
    }

    infowindow.setContent('<div id="infowindow"><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);
});

google.maps.event.addListener(autocomplete, 'place_changed', function () {
    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 = new google.maps.LatLng(
            position.coords.latitude, position.coords.longitude);

            autocomplete.setBounds(new google.maps.LatLngBounds(geolocation, geolocation));
        });
    }
}

var componentForm是您指定要从API检索哪些自动完成组件的地方。

我试图做的是查看是否在自动完成ie中只能选择完整地址,假设您需要为包交付选择完整地址,应该没有选择伦敦的选项。我检查了JSFIDLE,选择后隔离元素是不够的。我认为谷歌不允许这种地址隔离,因为还有其他提供商。感谢你的努力和回应。这并不能回答问题。可悲的是,我真的可以用一个答案来回答这个问题,但还没有找到任何其他与这个主题相关的问题。嘿@paul,你找到了限制它的方法吗?