2017-06-21 8 views
0

私は最初にGoogle Places APIにリンクされているいくつかのアドレスフィールドを持っています。したがって、このボックスにアドレスを入力すると、そのアドレスをクリックするとgoogle APIによってアドレスが他のアドレスフィールドに分割されます。ユーザーがこれを行うと、フォームからすべての情報を動的に収集するボタンをクリックします。ページが最初にロードされると、すべてのコントロールが動的にロードされるので、固定値が使用できないため、これらのフィールドがページにロードされない場合があります。値が入力ボックスから抽出されない

データを収集すると、Google APIから読み込まれたフィールドは空のまま残りますが、他のフィールドはすべて必要なものが入力されます。ここで

は、クリックイベントを追加し、APIの機能を管理JSです:

//#region API - Add Job 

$('#addJob').click(function() { 
    // Setup the object to pass to API 
    var Job = {}; 
    $(".form__input").each(function() { 
     var name = this.name; 
     var value = this.value; 
     Job[name] = value; 
    }); 
    console.log(Job); 

    // Pass Job Object to the API 
}); 

//#endregion API - Add Job 



//#region Address Auto Complete 

var placeSearch, autocomplete; 
var componentForm = { 
    street_number: 'short_name', 
    route: 'long_name', 
    locality: '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; 
     } 
    } 
} 

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()); 
     }); 
    } 
} 

//#endregion Address Auto Complete 

それはほとんどの入力フィールドのようだ値を持ちませんが、私はそれがページ上で実際にあります見ることができます。生成されたHTMLコードは次のとおりです。

<input name="CollectionAddress" class="form__input js-required" id="autocomplete" onfocus="geolocate()" type="text" placeholder="Collection Address" autocomplete="off"> 
<input name="CollectionAddressLine1" class="form__input js-required" id="street_number" placeholder="No."> 
<input name="CollectionAddressLine2" class="form__input js-required" id="route" placeholder="Street"> 
<input name="CollectionAddressLine3" class="form__input js-required" id="locality" placeholder="City"> 
<input name="CollectionPostcode" class="form__input js-required" id="postal_code" placeholder="Postcode"> 

、出力は '仕事' オブジェクトからである:

CollectionAddress: "123 Bradwel...", 
CollectionAddressLine1: "", 
CollectionAddressLine2: "", 
CollectionAddressLine3: "", 
CollectionPostcode: "" 
+0

コードを実行するとうまく動作します。https://jsfiddle.net/x0w8v4ta/ –

+0

Googleプレイスの自動補完機能を使用していません –

答えて

1

$(document).ready(function(){ 
 

 
\t \t $('#addJob').click(function() { 
 
\t \t // Setup the object to pass to API 
 
\t \t var Job = {}; 
 
\t \t $(".form__input").each(function() { 
 
\t \t  var name = this.name; 
 
\t \t  var value = this.value; 
 
\t \t  Job[name] = value; 
 
\t \t }); 
 
\t \t console.log(Job); 
 
     $("#result").html(JSON.stringify(Job)); 
 

 
\t \t // Pass Job Object to the API 
 
\t \t }); 
 

 
\t \t var placeSearch, autocomplete; 
 
\t \t var componentForm = { 
 
\t \t  street_number: 'short_name', 
 
\t \t  route: 'long_name', 
 
\t \t  locality: 'long_name', 
 
\t \t  postal_code: 'short_name' 
 
\t \t }; 
 
\t \t function initAutocomplete() { 
 
\t \t  // Create the autocomplete object, restricting the search to geographical 
 
\t \t  // location types. 
 
\t \t  autocomplete = new google.maps.places.Autocomplete(
 
\t \t   (document.getElementById('autocomplete')), 
 
\t \t   { types: ['geocode'] }); 
 

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

 
\t \t function fillInAddress() { 
 
\t \t  // Get the place details from the autocomplete object. 
 
\t \t  var place = autocomplete.getPlace(); 
 

 
\t \t  for (var component in componentForm) { 
 
\t \t   document.getElementById(component).value = ''; 
 
\t \t   document.getElementById(component).disabled = false; 
 
\t \t  } 
 

 
\t \t  // Get each component of the address from the place details 
 
\t \t  // and fill the corresponding field on the form. 
 
\t \t  for (var i = 0; i < place.address_components.length; i++) { 
 
\t \t   var addressType = place.address_components[i].types[0]; 
 
\t \t   if (componentForm[addressType]) { 
 
\t \t    var val = place.address_components[i][componentForm[addressType]]; 
 
\t \t    document.getElementById(addressType).value = val; 
 
\t \t   } 
 
\t \t  } 
 
\t \t } 
 

 
\t \t function geolocate() { 
 
\t \t  if (navigator.geolocation) { 
 
\t \t   navigator.geolocation.getCurrentPosition(function (position) { 
 
\t \t    var geolocation = { 
 
\t \t     lat: position.coords.latitude, 
 
\t \t     lng: position.coords.longitude 
 
\t \t    }; 
 
\t \t    var circle = new google.maps.Circle({ 
 
\t \t     center: geolocation, 
 
\t \t     radius: position.coords.accuracy 
 
\t \t    }); 
 
\t \t    autocomplete.setBounds(circle.getBounds()); 
 
\t \t   }); 
 
\t \t  } 
 
\t \t } 
 
\t \t initAutocomplete(); 
 
\t \t //#endregion Address Auto Complete 
 
\t \t });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&sensor=false"></script> 
 

 
<input name="CollectionAddress" class="form__input js-required" id="autocomplete" onfocus="" type="text" placeholder="Collection Address" autocomplete="off"> 
 
<input name="CollectionAddressLine1" class="form__input js-required" id="street_number" placeholder="No."> 
 
<input name="CollectionAddressLine2" class="form__input js-required" id="route" placeholder="Street"> 
 
<input name="CollectionAddressLine3" class="form__input js-required" id="locality" placeholder="City"> 
 
<input name="CollectionPostcode" class="form__input js-required" id="postal_code" placeholder="Postcode"> 
 
<button id="addJob">addJob</button> 
 

 
<div id="result"></div>

これが正常に動作しています。 "addJob"ボタンをクリックすると、すべての値を取得できます。

関連する問題