bp_ps_autocomplete_script( $f )

Output BuddyBoss profile search autocomplete script.

Description

Source

File: bp-core/profile-search/bps-templates47.php

358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
function bp_ps_autocomplete_script( $f ) {
    wp_enqueue_script( $f->script_handle );
    $autocomplete_options = apply_filters( 'bp_ps_autocomplete_options', "{types: ['geocode']}", $f );
    $geolocation_options  = apply_filters( 'bp_ps_geolocation_options', "{timeout: 5000}", $f );
    ?>
    <input type="hidden" id="Lat_<?php echo $f->unique_id; ?>" name="<?php echo $f->code . '[lat]'; ?>" value="<?php echo $f->value['lat']; ?>">
    <input type="hidden" id="Lng_<?php echo $f->unique_id; ?>" name="<?php echo $f->code . '[lng]'; ?>" value="<?php echo $f->value['lng']; ?>">
 
    <script>
        function bp_ps_<?php echo $f->unique_id; ?>() {
            var input = document.getElementById('<?php echo $f->unique_id; ?>');
            var options = <?php echo $autocomplete_options; ?>;
            var autocomplete = new google.maps.places.Autocomplete(input, options);
            google.maps.event.addListener(autocomplete, 'place_changed', function () {
                var place = autocomplete.getPlace();
                document.getElementById('Lat_<?php echo $f->unique_id; ?>').value = place.geometry.location.lat();
                document.getElementById('Lng_<?php echo $f->unique_id; ?>').value = place.geometry.location.lng();
            });
        }
 
        jQuery(document).ready(bp_ps_<?php echo $f->unique_id; ?>);
 
        function bp_ps_locate_<?php echo $f->unique_id; ?>() {
            if (navigator.geolocation) {
                var options = <?php echo $geolocation_options; ?>;
                navigator.geolocation.getCurrentPosition(function (position) {
                    document.getElementById('Lat_<?php echo $f->unique_id; ?>').value = position.coords.latitude;
                    document.getElementById('Lng_<?php echo $f->unique_id; ?>').value = position.coords.longitude;
                    bp_ps_address_<?php echo $f->unique_id; ?>(position);
                }, function (error) {
                    alert('ERROR ' + error.code + ': ' + error.message);
                }, options);
            } else {
                alert('ERROR: Geolocation is not supported by this browser');
            }
        }
 
        jQuery('#Btn_<?php echo $f->unique_id; ?>').click(bp_ps_locate_<?php echo $f->unique_id; ?>);
 
        function bp_ps_address_<?php echo $f->unique_id; ?>(position) {
            var geocoder = new google.maps.Geocoder;
            var latlng = {lat: position.coords.latitude, lng: position.coords.longitude};
            geocoder.geocode({'location': latlng}, function (results, status) {
                if (status === 'OK') {
                    if (results[0]) {
                        document.getElementById('<?php echo $f->unique_id; ?>').value = results[0].formatted_address;
                    } else {
                        alert('ERROR: Geocoder found no results');
                    }
                } else {
                    alert('ERROR: Geocoder status: ' + status);
                }
            });
        }
    </script>
    <?php
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.