Use external address lookup APIs
Use external address lookup APIs (getaddress.io)
You must define javascript function wpo_get_address_autocomplete_init.
Sample code, you must replace YOUR_API_KEY with your key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
add_filter('wpo_custom_init_autocomplete_js_function', function () { return 'wpo_get_address_autocomplete_init'; }); add_filter('wpo_custom_init_autocomplete_placeholder', function () { return 'Search by Postcode'; }); add_action('wpo_before_render_html_app', function () { ?> <style><span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span> .wpo-custom-autocomplete-list { position: absolute; background-color: white; width: 96%; padding: 10px; z-index: 100; max-height: 200px; overflow-y: scroll; } .wpo-custom-autocomplete-list li { cursor: pointer; } </style> <script src="https://getaddress.io/js/jquery.getAddress-2.0.8.min.js"></script> <!-- Add after your form --> <script> function wpo_get_address_autocomplete_init(elem, selectedCallback) { var on_select = function (v) { selectedCallback({ address_1: v.line_1, address_2: v.line_2 || v.line_3 || v.line_4 || v.locality, city: v.town_or_city, country: "GB", lat: v.latitude, lng: v.longitude, postcode: v.postcode, state: v.county, street_number: v.building_number, }); jQuery(elem).val('').trigger('input'); } jQuery(elem).on('input', function () { if (!jQuery(this).val()) { jQuery(elem).siblings('.wpo-custom-autocomplete-list').remove(); return; } jQuery.get( 'https://api.getaddress.io/find/' + jQuery(this).val(), { 'api-key': 'YOUR_API_KEY', expand: true, }, function (d) { var ul = jQuery('<ul class="wpo-custom-autocomplete-list"></ul>'); d.addresses.forEach(function (v, i) { ul.append(jQuery('<li data-index="'+ i +'">'+ (v.formatted_address.filter(function () { return !!v; }).join(' ')) +'</li>').on('click', function () { on_select( Object.assign({}, v, { latitude: d.latitude, longitude: d.longitude, postcode: d.postcode, }) ); }) ); }); jQuery(elem).siblings('.wpo-custom-autocomplete-list').remove(); jQuery(elem).after(ul); }, 'json'); }); } </script> <?php }); |