4

I've successfully integrated the Geocomplete jquery plugin on a wordpress site. My issue is that I cannot limit the autosuggestion results to neighbourhood or sublocality. I've revised the Google Place types thoroughly but my results are always just the same. I know I'm doing something incorrectly somewhere. Here's what I have so far:

add_action( 'wp_head', 'add_geocomplete_script' );

function add_geocomplete_script() {
   ?>
   <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCJ1Bgyx1jSN3aRFB3uUkLLntXCONZfOyM&libraries=places"></script>
   <script src="<?php echo get_stylesheet_directory_uri() ?>/js/jquery.geocomplete.js"></script>
   <script src="<?php echo get_stylesheet_directory_uri() ?>/js/logger.js"></script>
   <?php 
} 

function woocommerce_checkout_autocomplete(){
?>
    <script>
        jQuery(document).ready(function() {

            var options = {
                //types: ['(cities)'],
                //types: ['sublocality'],
                //componentTypes: ['neighborhood'],
                types: ["geocode", "sublocality"],
                country: 'za'
            };

            jQuery("#billing_myfield8").geocomplete(options);

            jQuery("#billing_myfield8").on("keyup", function() {
                //jQuery("#billing_myfield8").trigger("geocode");
                console.log('You typed in the input box');
            });
        });

    </script>

<?php
}

add_action( 'wp_footer', 'woocommerce_checkout_autocomplete' );

With the above code I'm still able to search for an address, eg. 112 Nowhere Street. It doesn't filter the results.

Landman
  • 41
  • 2

1 Answers1

1

First of all, please note of the deprecation notice in Place Autocomplete wherein it states that

The types parameter is deprecated as of February 16, 2016, replaced by a new type parameter which only supports one type per search request.

With this, I would suggest that you use type parameter to restrict the results to places matching the specified type. Only one type may be specified (if more than one type is provided, all types following the first entry are ignored).

Additionally, from the list of supported types for place autocomplete requests, you can use regions type to return any result matching sublocality.

Lastly, it will help if you go through Place Searches documentation and also check solution given in this SO post.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22