Back
Step 1/3
Select a service

How can we help?

Let’s turn those What IF’s into reality, select one or more services to get started.

Next
Contact Details

How can we reach you?

Final step! Fill out your contact info, and we’ll be in touch to discuss your project soon.

Leave blank if you don’t have one yet
Next
project details

Tell us about
the project

Tell us everything! Share your thoughts, ideas, and any specifics about your project. The more details, the better.

Thank you for contacting us!

We will be in touch soon

Keep your eyes peeled for an email from one of our founders.

Okay, got it!
Oops! Something went wrong while submitting the form.

Google Places Autocomplete

Suggests Google addresses as users type and ensures they select a valid one.

Javascript

Copy Code

<!-- Google Places -->
<script src="https://maps.googleapis.com/maps/api/js?key=APIKEY&libraries=places"></script>

<!-- Google Places Address Autocomplete -->
<script>
document.addEventListener('DOMContentLoaded', function() {
  // Find all address inputs with this class
  const addressInputs = document.querySelectorAll('.address-autocomplete');
  
  addressInputs.forEach(addressInput => {
    if (addressInput) {
      const autocomplete = new google.maps.places.Autocomplete(addressInput, {
        types: ['address'],
        componentRestrictions: { country: 'nz' }
      });
      
      // Create a hidden validation field
      const validationField = document.createElement('input');
      validationField.type = 'text';
      validationField.name = `${addressInput.id}-validation`;
      validationField.required = true;
      validationField.style.display = 'none';
      addressInput.parentNode.appendChild(validationField);
      
      autocomplete.addListener('place_changed', function() {
        const place = autocomplete.getPlace();
        
        if (place.formatted_address) {
          addressInput.value = place.formatted_address;
          validationField.value = 'valid';
          addressInput.setCustomValidity('');
        } else {
          validationField.value = '';
          addressInput.setCustomValidity('Please select a valid address from the dropdown');
        }
      });
      
      addressInput.addEventListener('input', function() {
        validationField.value = '';
        addressInput.setCustomValidity('Please select an address from the dropdown');
      });
    }
  });
});
</script>

How to set it up

  • Create a Google Cloud Project
  • Enable the APIs
    • Navigate to APIs & Services -> Library.
    • Enable Places API.
  • Generate an API Key
    • Go to APIs & Services -> Credentials -> Create Credentials -> API Key
    • Copy the key
  • Restrict the API Key
    • Click the pencil icon next to your API key and under key restrictions, select HTTP referrers.
    • Enter the website with a "*" e.g. https://yourdomain.com/* where this key will be used. The "*" means all pages on your website are allowed.
  • Include the Google Places script in your <head> code
  • Add these class name .address-autocomplete to your address input field.
  • Add the Google Places Address Autocomplete script to your <body> code and insert your API key (where the current code says 'APIKEY'.

How it works

  • When you start typing
    • Google watches what the user types and shows matching addresses.
  • When you pick an address
    • The box fills with the full address.
    • The website knows it's a valid address.
  • If you don't pick from the list
    • The website asks you to choose one from the dropdown, this prevents mistakes like typos.
  • Country restriction
    • The current code will be limited to New Zealand only so users don't pick addresses from other countries. You can change this by changing the 'country' value.

Fancy a Free Quote?

Got a crazy idea? We’re all ears. Reach out, share your story, and let’s make some magic together. Click below to get your free quote.

Go on, it's FREE
Forms not your thing, book a call instead