Restrict Bounds of Autocomplete Results

By default, Address Autocomplete can only limit the country for which results are returned. It will also bias results towards the user’s current location. Use this snippet to define a circular area to limit results to by specifying your area’s center latitude and longitude and the radius in miles of your circular area.

Code

Filename: gpaa-restrict-bounds.php

<?php
/**
 * Gravity Wiz // Address Autocomplete // Restrict Bounds of Autocomplete Results
 * https://gravitywiz.com/documentation/gravity-forms-address-autocomplete/
 *
 * By default, Address Autocomplete can only limit the country for which results are returned.
 * It will also bias results towards the user's current location. Use this snippet to define a
 * circular area to limit results to by specifying your area's center latitude and longitude and
 * the radius in miles of your circular area.
 */
// Update "123" to the ID of your form.
add_action( 'gform_pre_enqueue_scripts_123', function() {
	?>
	<script>
		gform.addFilter( 'gpaa_autocomplete_options', function( options, gpaa, formId ) {

			// Update miles to the average
			var miles     = 100;
			var centerLat = 34.5199;
			var centerLng = -105.8701;

			var metersInMile = 1609.344;
			var autocompleteCircleBounds = new google.maps.Circle( { center: new google.maps.LatLng( centerLat, centerLng ), radius: miles * metersInMile } );

			options.bounds       = autocompleteCircleBounds.getBounds();
			options.strictBounds = true;

			return options;
		} );
	</script>
	<?php
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.