2014-11-21 1 views
2

사용자로부터 위치 정보를 수집하는 양식이 있습니다. 위치 정보를 자동 완성하기 위해 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)); 
     }); 
    } 
} 

스크린 샷

enter image description here

이 버그 또는 단순히 구글이 건물의 우편 번호를 모르는 것이 있을까. 이 경우 주변에 어떤 작업이 있습니까?

미리 감사드립니다.

+4

구글의 자신의 예를 해당 주소에 대한 우편 번호를 찾을 수 없습니다. 따라서 붙여 넣기 된 상용구 코드의 벽과 관련이 없습니다. 이 질문은 Google에 보내는 질문과 같습니다. – bzlm

답변

0

이 주소에는 우편 번호가 있지만 일부만 (FSA 코드라고 생각합니다).

["postal_code_prefix", "postal_code"] 

을하지만 함수 유형 만 배열의 첫 번째 항목을 확인 :

이 주소에 반환 유형 배열이기 때문에 기능이 부분을 인식하지 않았다.

가능 수정 :

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 addressTypes = place.address_components[i].types,addressType,val; 
    for(var j=0;j<addressTypes.length;++j){ 

     addressType=addressTypes[j]; 

     if (componentForm[addressType]) { 
     val = place.address_components[i][componentForm[addressType]]; 
     document.getElementById(addressType).value = val; 
     break; 
     } 
    } 

    } 
} 
+0

아직 작동하지 않습니다. 어떤 FSA 코드를 받았습니까? V7X? – TheBokiya

+0

예, 'V7X' http://jsfiddle.net/doktormolle/9bLcu2cd/ –

+0

실제로 건물의 FSA 코드가 올바르지 않습니다. Google지도는 또한 Google지도에서 주소를 찾는 경우 해당 코드 만 제공합니다. – TheBokiya

0
var location = place.geometry.location; 
var lat = location.lat(); 
var lng = location.lng(); 


var latlng = {lat: lat, lng: lng}; 
      geocoder.geocode({'location': latlng}, function(results, status) { 
         if (status == google.maps.GeocoderStatus.OK) { 

          if (results[0]) { 

          for (var i = 0; i < results[0].address_components.length; i++) { 
           var types = results[0].address_components[i].types; 

           for (var typeIdx = 0; typeIdx < types.length; typeIdx++) { 
            if (types[typeIdx] == 'postal_code') { 
             //console.log(results[0].address_components[i].long_name); 

             var pincode = results[0].address_components[i].short_name; 
             alert('pin'+pincode); 

            } 
           } 
          } 

          $('#pac-input2').val(results[0].formatted_address); 
          infowindow.setContent(results[0].formatted_address); 
          //infowindow.open(map, marker); 
          } 
         } 

      }); 
+0

스택 오버플로에 오신 것을 환영합니다! 이 코드가 질문에 대답 할 수는 있지만 이유 및/또는이 코드가 질문에 어떻게 대답하는지에 대한 추가 컨텍스트를 제공하면 장기적인 가치가 향상됩니다. 코드가없는 대답은 아무것도 사용하지 않는 것이 좋습니다. – Ajean

관련 문제