사용자로부터 위치 정보를 수집하는 양식이 있습니다. 위치 정보를 자동 완성하기 위해 Google 장소 API를 사용하고 있습니다. 그것은 위대한 작품. 모든 것이 멋지게 채워졌지만 일부 장소 (예 : 건물 인 직장)가 있으면 Google에서 우편 번호를 반환하지 않습니다.Google은 일부 장소에 대해 우편 번호를 반환하지 않는 API를 배치합니다.
Google 주소 자동 완성에서이 example을 따라했습니다. 그것은 또한 내 직장을위한 우편 번호를 반환하지 않습니다. 그래서 나는 내가 잘못한 것을 완전히 확신하지 못한다. 다음은
내 코드입니다 :HTML
<div id="location-autocomplete" style="display: none;">
<div class="half-formfield">
@Html.LabelFor(x => x.Address)
@Html.TextBoxFor(x => x.Address, new { @class = "two-third-field", id = "street_number", required = "required" })
</div>
<div>
@Html.LabelFor(x => x.UnitNumber, new { value = "Unit Number" })
@Html.TextBoxFor(x => x.UnitNumber, new { @class = "one-third-field" })
</div>
<div class="half-formfield">
@Html.LabelFor(x => x.City)
@Html.TextBoxFor(x => x.City, new { @class = "half-field", required = "required", id = "locality" })
</div>
<div>
@Html.LabelFor(x => x.ProvinceOrState)
@Html.TextBoxFor(x => x.ProvinceOrState, new { @class = "half-field", required = "required", id = "administrative_area_level_1" })
</div>
<div class="half-formfield">
@Html.LabelFor(x => x.ZipOrPostalCode)
@Html.TextBoxFor(x => x.ZipOrPostalCode, new { @class = "half-field", required = "required", id = "postal_code" })
</div>
<div>
@Html.LabelFor(x => x.Country)
@Html.TextBoxFor(x => x.Country, new { @class = "half-field", required = "required", id = "country" })
</div>
<input type="button" value="Clear Address" onclick="clearAddress()" id="clear-btn" class="service-form-btn"/>
JS
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initialize() {
// Create the autocomplete object, restricting the search
// to geographical location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */(document.getElementById('autocomplete')),
{ types: ['geocode'] });
// When the user selects an address from the dropdown,
// populate the address fields in the form.
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
showAddressComponent();
setTimeout(hideAddressSearch, 800);
});
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
if (component != "route") {
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]];
console.log(val);
if (addressType != "route") document.getElementById(addressType).value = val;
else if (addressType == "route") document.getElementById("street_number").value = document.getElementById("street_number").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));
});
}
}
스크린 샷
이 버그 또는 단순히 구글이 건물의 우편 번호를 모르는 것이 있을까. 이 경우 주변에 어떤 작업이 있습니까?
미리 감사드립니다.
구글의 자신의 예를 해당 주소에 대한 우편 번호를 찾을 수 없습니다. 따라서 붙여 넣기 된 상용구 코드의 벽과 관련이 없습니다. 이 질문은 Google에 보내는 질문과 같습니다. – bzlm