2016-07-21 12 views
0

Ich muss die Adresse Werte wie Adresse, Stadt, Postleitzahl, etc. Zu meinem @Modell, wo ich Felder haben, um die Werte zu empfangen.Rails Google Orte API-Adresse Autocomplete. Pass Address Werte in @Model

Ich habe gekämpft, viele verschiedene Dinge zu versuchen, einschließlich einiger Ajax, die wie chinesisch aussah ....., die letzten 3 Tage. Ich brauche Hilfe. Wenn Sie die Zeit haben, scheint es viele Menschen mit dem gleichen zu kämpfen. Id Liebe, wenn jemand eine vollständige Lösung/Tutorial bieten könnte.

Auto-Vervollständigungen funktioniert gut, die Google-Felder mit den verschiedenen Elementwerten füllen. Ich dachte, ich könnte erb hidden_fields verwenden und die html-Werte meinen erb-Formularfeldern zuweisen, aber ich kann es nicht zum Laufen bringen.

In meiner regelmäßigen erb Form habe ich das API Webformular Google Places:

Start Address auto-complete 
<style> 
    html, body { 
    height: 100%; 
    margin: 0; 
    padding: 0; 
    } 
    #map { 
    height: 100%; 
    } 
</style> 
<link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> 
<style> 
    #locationField, #controls { 
    position: relative; 
    width: 480px; 
    } 
    #autocomplete { 
    position: absolute; 
    top: 0px; 
    left: 0px; 
    width: 99%; 
    } 
    .label { 
    text-align: right; 
    font-weight: bold; 
    width: 100px; 
    color: #303030; 
    } 
    #address { 
    border: 1px solid #000090; 
    background-color: #f0f0ff; 
    width: 480px; 
    padding-right: 2px; 
    } 
    #address td { 
    font-size: 10pt; 
    } 
    .field { 
    width: 99%; 
    } 
    .slimField { 
    width: 80px; 
    } 
    .wideField { 
    width: 200px; 
    } 
    #locationField { 
    height: 20px; 
    margin-bottom: 2px; 
    } 
</style> 


<div id="locationField"> 
    <input id="autocomplete" placeholder="Enter your address" 
     onFocus="geolocate()" type="text"></input> 
</div> 

<table id="address"> 
    <tr> 
    <td class="label">Calle</td> 
    <td class="slimField"><input class="field" id="street_number" 
      disabled="true"></input></td> 
    <td class="wideField" colspan="2"><input class="field" id="route" 
      disabled="true"></input> 
      </td> 
    </tr> 
    <tr> 
    <td class="label">Ciudad</td> 
    <td class="wideField" colspan="3"><input class="field" id="locality" 
      disabled="true"></input></td> 
    </tr> 
    <tr> 
    <td class="label">Provincia</td> 
    <td class="wideField" colspan="3"><input class="field" id="administrative_area_level_2" 
      disabled="true"></input></td> 
    </tr> 

    <tr> 
    <td class="label">subloc</td> 
    <td class="wideField" colspan="3"><input class="field" id="sublocality" 
      disabled="true"></input></td> 
    </tr> 

    <tr> 
    <td class="label">geocode</td> 
    <td class="wideField" colspan="3"><input class="field" id="geocode" 
      disabled="true"></input></td> 
    </tr> 

    <tr> 
    <td class="label">neighborhood</td> 
    <td class="wideField" colspan="3"><input class="field" id="neighborhood" 
      disabled="true"></input></td> 
    </tr> 
    <tr> 
    <td class="label">street address</td> 
    <td class="wideField" colspan="3"><input class="field" id="street_address" 
      disabled="true"></input></td> 
    </tr> 

    <tr> 
    <td class="label"></td> 
    <td class="slimField"><input class="field" 
      id="administrative_area_level_1" disabled="true"></input></td> 
    <td class="label">Codigo Postal</td> 
    <td class="wideField"><input class="field" id="postal_code" 
      disabled="true"></input></td> 
    </tr> 
    <tr> 
    <td class="label">Pais</td> 
    <td class="wideField" colspan="3"><input class="field" 
      id="country" disabled="true"></input></td> 
    </tr> 
</table> 


<!-- 
    // This example displays an address form, using the autocomplete feature 
    // of the Google Places API to help users fill in the information. 

    // This example requires the Places library. Include the libraries=places 
    // parameter when you first load the API. For example: 
    // <script src="https://maps.googleapis.com/maps/api/js?key=[MY_KEY]&libraries=places"></script> 
--> 

    <script type="text/javascript"> 
    var placeSearch, autocomplete; 
    var componentForm = { 
    street_number: 'short_name', 
    route: 'long_name', 
    locality: 'long_name', 
    administrative_area_level_1: 'short_name', 
    administrative_area_level_2: 'short_name', 
    country: '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(
     /** @type {!HTMLInputElement} */(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()); 
     }); 
    } 
    } 
</script> 



<script src="https://maps.googleapis.com/maps/api/js?key=[MY_KEY]&libraries=places&callback=initAutocomplete" 
    async defer></script> 


<!-- End Address auto-complete --> 
+0

erhalten Sie die Werte aus den Adresskomponenten und haben Sie die richtigen ID-Felder? – Sravan

+0

Ich bekomme die Werte von Google, Meine Felder sind anders benannt. – Francisco

Antwort

0

Wenn Sie die richtigen Daten in dem address_attributes verwenden, hier ist ein einfaches Beispiel, dies zu tun.

$(function() { 
    $("#address").geocomplete({ 
     details: ".geo-details", 
     detailsAttribute: "geo" 
    }); 
}); 

$(function() { 
    $("#address") 
     .geocomplete() 
     .bind("geocode:result", function (event, result) {      
      b = result 
      a = result.address_components 
      for (var i = 0, l = a.length; i < l; i++) { 
       if (a[i].types.indexOf("country") > -1) 
       { 
        $('#address_line').val(a[i].long_name) 
       } 
       if (a[i].types.indexOf("postal_code") > -1) 
       { 
        $('#zipcode').val(a[i].long_name) 
       } 
       if (a[i].types.indexOf('administrative_area_level_1') > -1) 
       { 
        $('#state').val(a[i].long_name) 
       } 

      } 
       $("#latitude").val(result.geometry.location.lat()); 
       $("#longitude").val(result.geometry.location.lng()); 
    }); 
}); 

Dann wird die Form der enthalten sollte, address_line, zipcode, state, latitude, and longitude id Elemente

+0

Sie sagen, das Formular sollte die ID-Elemente enthalten, Sie meinen, dass dies meine Felder sind oder ich meine Felder auf diese IDs zeigen sollte? – Francisco

+0

zeigen Sie Ihre Felder auf diese IDs und Sie werden die Werte in die Felder durch die IDs – Sravan

+0

Um auf die ID zeigen, wenn ich <% = f.text_field: myaddresslinefield, ID: "address_line"%> Ich bekomme keine Fehler , aber der Wert wird nicht gespeichert. Wenn ich <% = f.text_field: myaddresslinefield, id = "address_line"%> verwende, bekomme ich einen Fehler: ActionView :: Template :: Error (undefinierte Methode 'merge 'für" address_line ": String): Sollte ich auf die ID anders? tks – Francisco

0

ich es endlich gelöst simple_form Gem

Hier mein Code ist. Ich sammle die ganze Adresse in einem Feld (getrennt durch Kommas), und ich sammle dann alle verschiedenen Elemente auch separat. Aufgrund meines Layouts habe ich versteckte Erb-Eingaben verwendet, aber sie werden live eingefügt, wenn Sie den Teil ",: as =>: hidden" entfernen.

Hoffe, das hilft einigen von euch, es war eine schwierige für mich, es herauszufinden! Genießen!!

<%= simple_form_for(@mymodel) do |f| %> 

<!-- The order of erb inputs before html inputs seems to be important! had problems changing it and couldnt remove html inputs either. --> 

<%= f.input :my_model_address, :label => "Direccion", :input_html =>{:id => 'autocomplete'}, :placeholder => 'Empieza con el nombre de la calle...' %> 
<%= f.input :my_model_street, :as => :hidden, :as => :hidden, :input_html =>{:id => 'route'} %> 
<%= f.input :my_model_street_number, :as => :hidden, :input_html =>{:id => 'street_number'} %> 
<%= f.input :my_model_postal_code, :as => :hidden, :input_html =>{:id => 'postal_code'} %> 
<%= f.input :my_model_city, :as => :hidden, :input_html =>{:id => 'locality'} %> 
<%= f.input :my_model_state, :as => :hidden, :input_html =>{:id => 'administrative_area_level_2'} %> 
<%= f.input :my_model_country, :as => :hidden, :input_html =>{:id => 'country'} %> 

<input type="hidden" id="autocomplete" placeholder="Enter your address" onFocus="geolocate()" type="text"></input> 
<input type="hidden" class="field" id="street_number" disabled="true"></input> 
<input type="hidden" class="field" id="route" disabled="true"></input> 
<input type="hidden" class="field" id="locality" disabled="true"></input> 
<input type="hidden" class="field" id="administrative_area_level_2" disabled="true"></input> 
<input type="hidden" class="field" id="street_address" disabled="true"></input></td> 
<input type="hidden" class="field" id="administrative_area_level_1" disabled="true"></input></td> 
<input type="hidden" class="field" id="postal_code" disabled="true"></input></td> 
<input type="hidden" class="field" id="country" disabled="true"></input></td> 

<script type="text/javascript"> 
    var placeSearch, autocomplete; 
    var componentForm = { 
    street_number: 'short_name', 
    route: 'long_name', 
    locality: 'long_name', 
    administrative_area_level_1: 'short_name', 
    administrative_area_level_2: 'short_name', 
    country: '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(
     /** @type {!HTMLInputElement} */(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()); 
     }); 
    } 
    } 
</script> 

<script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places&callback=initAutocomplete" 
    async defer></script> 

<!-- End Address auto-complete -->