Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Autocompleted Address breakdown into multiple fields

More
5 years 11 months ago #188683 by davidg1982
How do I add Lat and Long to the script?

I am not sure how to do that.
The topic has been locked.
More
5 years 11 months ago - 5 years 11 months ago #188687 by tpartner
You can use the place.geometry.location.lat() and place.geometry.location.lng() functions:

Code:
<script>
  function init() {
    $('#displayCounty').hide();
 
    // Disable the partial-address inputs
    $('#question{QID} input:text:gt(0)').prop('readonly', true);
 
    // Un-comment below to hide the partial-address inputs
    //$('#question{QID} input:text:gt(0)').hide();
 
    var input = document.getElementById('answer{SID}X{GID}X{QID}SQ001');
    var autocomplete = new google.maps.places.Autocomplete(input);
 
    autocomplete.addListener('place_changed', function() {
      var place = autocomplete.getPlace();
      var components = place.address_components;
      if (components) {
        for (var i = 0, l = components.length; i < l; i++) {
          var component = components[i];
          if (component.types &amp;&amp; component.types.indexOf('street_number') !== -1) {
            $('#answer{SID}X{GID}X{QID}SQ002').val(component.long_name);
          }
          if (component.types &amp;&amp; component.types.indexOf('route') !== -1) {
            $('#answer{SID}X{GID}X{QID}SQ003').val(component.long_name);
          }
          if (component.types &amp;&amp; component.types.indexOf('locality') !== -1) {
            $('#answer{SID}X{GID}X{QID}SQ004').val(component.long_name);
          }
          if (component.types &amp;&amp; component.types.indexOf('administrative_area_level_2') !== -1) {
            $('#displayCounty').text('County: ' + component.long_name);
            $('#displayCounty').show();
            $('#answer{SID}X{GID}X{QID}SQ005').val(component.long_name);
          }
          if (component.types &amp;&amp; component.types.indexOf('administrative_area_level_1') !== -1) {
            $('#answer{SID}X{GID}X{QID}SQ006').val(component.long_name);
          }
          if (component.types &amp;&amp; component.types.indexOf('country') !== -1) {
            $('#answer{SID}X{GID}X{QID}SQ007').val(component.long_name);
          }
          if (component.types &amp;&amp; component.types.indexOf('postal_code') !== -1) {
            $('#answer{SID}X{GID}X{QID}SQ008').val(component.long_name);
          }
          $('#answer{SID}X{GID}X{QID}SQ009').val(place.geometry.location.lat(place.address_components.formatted_address));
          $('#answer{SID}X{GID}X{QID}SQ010').val(place.geometry.location.lng(place.address_components.formatted_address));
        }
      }
    });
  }
  google.maps.event.addDomListener(window, 'load', init);
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...7672.lss
File Size:23.28 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 11 months ago by tpartner.
The following user(s) said Thank You: davidg1982
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose