How to Target Gravity Forms Address Autocomplete to Specific Locations

Show Address Autocomplete suggestions only for the places you actually need—by country, radius, city, or even by place name.

GP Address Autocomplete lets you type a few letters, get location suggestions, and fill an entire Address field with a single click. Pretty magical stuff.

Out of the box, it pulls in suggestions from all over the globe. Handy if you’re, for example, shipping worldwide. But if you only serve a specific area, you’ll want to narrow those results down so users only see addresses that’s relevant to them. 

Here’s what we’re covering:

  • Filtering by country
  • Restricting each field to a specific country 
  • Setting a radius boundary around an area
  • Autocompleting by city only
  • Searching places by name
  • Capturing apartment/unit numbers

Let’s teach your Address fields some boundaries—literally.

This article requires the Gravity Forms Address Autocomplete perk.

Buy Gravity Perks to get this perk plus 50 other premium Gravity Forms plugins!

View Plugin Buy Now

Filter by Country

First things first: make sure Address Autocomplete is enabled. If it’s your first time, check out our quick setup guide to get it up and running.

Let’s say you’re only shipping to Southeast Asia, or maybe you’re running a regional campaign. You can flip on the Countries filter to limit suggestions to specific countries. Once enabled, it’ll apply to every Address field on your site with Address Autocomplete enabled (or use one of the snippets below to tweak fields individually).

Here’s how:

  1. In your WordPress dashboard, go to FormsSettings Autocomplete.
  2. Under Countries, select the country/ies you need. (Google only supports up to 5 countries at a time).
The Address Autocomplete settings page shows a Google API key input field and a country selection dropdown with South East Asian countries visible in the list.

Set Location Boundaries

These next tricks let you fine-tune autocomplete suggestions to the area you need.  

Restrict By Country

Working with multiple Address fields? You can restrict each field to a specific country so users only see relevant suggestions. This is handy for scenarios like collecting shipping and billing addresses for different regions.

This trick uses JavaScript, so you’ll need Code Chest—our handy tool that lets you add and organize JavaScript and CSS snippets right in Gravity Forms. Just open your Spellbook › activate GF Code Chest.

gform.addFilter( 'gpaa_autocomplete_options', function( autocompleteOptions, gpaa, formId, fieldId ) {
	// Update "1" to your Address field ID.
	if ( formId != GFFORMID || fieldId != 1 ) {
		return autocompleteOptions;
	}
	if ( typeof autocompleteOptions.componentRestrictions !== 'object' ) {
		autocompleteOptions.componentRestrictions = {};
	}
	// Update "DE" to the country to which you would like to restrict results.
	autocompleteOptions.componentRestrictions.country = [ 'DE' ];
	return autocompleteOptions;
} );

How to use:

  1. Copy the snippet above.
  2. Open Code Chest and paste it inside the JavaScript box.
  3. Change 1 to your Address field ID.
  4. Replace DE with your Alpha-2 country code.
  5. Click Save Settings.

Note sure which code to use? Here’s a handy list of country codes.

A form showing two address fields. One field displays a completed address in Singapore, another field shows a dropdown with Malaysian address suggestions.

Set a Radius Boundary

This snippet lets you draw an invisible circle around any location. Just choose your center point, set the radius, and only addresses within that circle will show up. This is useful for local businesses that only serve a specific area—like a pizza shop that only delivers within five miles!

This snippet uses PHP, so you’ll need Code Snippets—a free WordPress plugin that lets you add PHP code easily. Just head to PluginsAdd New, search for Code Snippets, then install and activate.

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
} );

How to use:

  1. Copy the snippet above.
  2. Open Code Snippets, add a new snippet, and paste it.
  3. Change 123 to your form’s ID.
  4. Set miles to your desired radius.
  5. Set the centerLat (latitude) and centerLng (longitude) to your center point.
  6. Click Save and Activate.

If you’re not sure where to get coordinates, right-click any location in Google Maps and copy the latitude and longitude that appears.

Pro-tip

You can pair this with GP Bookings to create location-aware scheduling that only shows slots for your service area.

Autocomplete by City Only

Just need the city? Maybe you’re running a regional survey or grouping users by location. This snippet lets you capture only the city, state, and country.

You’ll need Code Snippets for this once since it uses PHP.

How to use

  1. Copy the full Autocomplete by City snippet here.
  2. Paste the snippet into Code Snippets.
  3. Scroll to the bottom of the code snippet and find this “Configuration” section:
# Configuration

new GPAA_Autocomplete_By_City( array(
	'form_id'  => 123, // Update "123" to your form ID.
	'field_id' => 4,   // Update "4" to your Address field ID.
) );
  1. Change 123 to your form’s ID.
  2. Change 4 to your Address field’s ID.
  3. Click Save and Activate.
A user types ‘Rome’ in the City field and selects the first suggestion. The address field autocompletes.

Target by Place Name and Unit Number

These tricks help you find the exact address by searching for a place’s name or by including the apartment or unit number. 📍

Search by Place Name

When booking appointments, ordering delivery, or visiting a place, people naturally think of destinations by name first (like “Starbucks” or “Central Park”) rather than the street address. You can do this by using a snippet that lets you search and show places by name, instead of typing the street address.

This one uses JavaScript, so you’ll need to use Code Chest.

gform.addFilter( 'gpaa_autocomplete_options', function( options ) {
	options.types = [ 'geocode', 'establishment' ];
	options.fields.push( 'name' );
	return options;
} );

// Display Place Name
gform.addAction( 'gpaa_fields_filled', function ( place, instance, formId, fieldId ) {
	let $addressLine1 = jQuery( '#input_{0}_{1}_1'.gformFormat( formId, fieldId ) );
	$addressLine1.val( place.name + ', ' + $addressLine1.val() ).trigger('change');
} );

How to use:

  1. Copy the snippet above.
  2. Open Code Chest and paste it inside the JavaScript box.

Click Save Settings.

A user types ‘The Wizarding World’ in the Street address field and selects the first suggestion. The address field autocompletes.

Always Capture Unit Numbers

In some countries, Google’s Places API leaves out the unit or apartment number when autocompleting an address. For example: “45 Collins Street, Melbourne” is different from “45 Collins Street, Unit 12, Melbourne.”

That missing unit number is critical for deliveries and billings, as packages can get lost or end up at the wrong door. This snippet makes sure the unit number is always included!

This trick uses JavaScript, so you’ll need to use Code Chest.

How to use:

  1. Copy the Always Use Unit Numbers snippet here.
  2. Open Code Chest and paste the snippet.
  3. Click Save Settings.

Done! Who says geography class is for humans only?

– Address fields  🗺️

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.


This field is for validation purposes and should be left unchanged.
Grab a bundle of free Gravity Forms plugins

Enter your email and receive our most popular free plugins and snippets, plus access to hundreds of others.