How to Add Address Autocomplete to a Vue 3 App

API or service
Address Autocomplete API + @geoapify/geocoder-autocomplete
Task
Typed text → address suggestions → selected address → Vue form fields
Examples
Vue 3JSAI
Difficulty
Beginner
Time
15 min

Add address autocomplete to a Vue 3 app

A user fills an address form in a Vue 3 app and should pick the address from suggestions instead of typing every field. Geoapify has no Vue-specific package, but the framework-independent @geoapify/geocoder-autocomplete library fits into a small Vue component. The library draws the input and the suggestion list and calls the Address Autocomplete API as the user types.

This guide builds an AddressAutocomplete single-file component that creates the library in onMounted, emits the selected address, and calls destroy() in onBeforeUnmount. A parent form then copies the street, house number, postcode, city, state, and country into v-model fields.

Task flow: typed text → address suggestions → selected GeoJSON feature → select event → reactive form fields.

Install the library and set the API key

Install the core library in a Vue 3 project, for example one created with npm create vue@latest or Vite:

npm install @geoapify/geocoder-autocomplete

Import one of the packaged themes once, in src/main.js. The package includes minimal.css, minimal-dark.css, round-borders.css, and round-borders-dark.css:

import { createApp } from "vue";
import App from "./App.vue";
import "@geoapify/geocoder-autocomplete/styles/minimal.css";

createApp(App).mount("#app");

Keep the API key out of the source code. In a Vite project, put it in .env.local; Vite exposes variables that start with VITE_ as import.meta.env.VITE_GEOAPIFY_API_KEY:

VITE_GEOAPIFY_API_KEY=YOUR_API_KEY

The key still reaches the browser, because the browser sends the autocomplete requests. Restrict it to your site's origins in Geoapify MyProjects.

Wrap the library in a Vue component

Create src/components/AddressAutocomplete.vue. The library needs an existing DOM element, so the component renders an empty container and creates the autocomplete in onMounted, when the element exists:

<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import { GeocoderAutocomplete } from "@geoapify/geocoder-autocomplete";

const props = defineProps({
  apiKey: { type: String, required: true },
  options: { type: Object, default: () => ({}) }
});
const emit = defineEmits(["select"]);

const container = ref(null);
let autocomplete = null;

onMounted(() => {
  // The library creates its own input and suggestion list inside the container
  autocomplete = new GeocoderAutocomplete(container.value, props.apiKey, props.options);

  // The selected place is a GeoJSON feature, or null when the field is cleared
  autocomplete.on("select", (place) => emit("select", place));
});

onBeforeUnmount(() => {
  autocomplete?.destroy();
  autocomplete = null;
});
</script>

<template>
  <div ref="container" class="address-autocomplete"></div>
</template>

<style scoped>
.address-autocomplete {
  position: relative;
}
</style>
  • The library instance is a plain variable, not a ref. Vue does not need to track it, and keeping it out of reactivity avoids wrapping the library's object in a proxy.
  • destroy() cancels active requests and removes the DOM listeners and markup the library owns. Call it whenever Vue removes the component, for example behind v-if or on a route change. A new instance is created the next time the component mounts.
  • position: relative on the container keeps the suggestion list under the input.
  • options takes the library options, such as placeholder, lang, limit, type, filter, and bias. The library reads them once, when it is created; the "React to option changes" step below shows how to change them later.

The library also emits input, suggestions, open, close, and other events. Forward the ones your form needs with the same autocomplete.on(...) → emit(...) pattern.

Fill a v-model form from the selected address

In the parent component, keep the address in a reactive object, bind each field with v-model, and copy the address parts from the selected feature in the select handler. src/App.vue:

<script setup>
import { reactive } from "vue";
import AddressAutocomplete from "./components/AddressAutocomplete.vue";

const apiKey = import.meta.env.VITE_GEOAPIFY_API_KEY;

const autocompleteOptions = {
  placeholder: "Start typing an address",
  limit: 5,
  skipIcons: true
};

const address = reactive({
  street: "",
  housenumber: "",
  postcode: "",
  city: "",
  state: "",
  country: "",
  lat: null,
  lon: null
});

function onAddressSelect(place) {
  if (!place) return; // the field was cleared: keep what the user entered
  const p = place.properties;
  address.street = p.street ?? "";
  address.housenumber = p.housenumber ?? "";
  address.postcode = p.postcode ?? "";
  address.city = p.city ?? p.town ?? p.village ?? "";
  address.state = p.state ?? "";
  address.country = p.country ?? "";
  address.lat = p.lat;
  address.lon = p.lon;
}
</script>

<template>
  <form class="address-form" @submit.prevent>
    <label>Search address</label>
    <AddressAutocomplete
      :api-key="apiKey"
      :options="autocompleteOptions"
      @select="onAddressSelect"
    />

    <label>Street <input v-model="address.street" autocomplete="address-line1" /></label>
    <label>House number <input v-model="address.housenumber" /></label>
    <label>Postcode <input v-model="address.postcode" autocomplete="postal-code" /></label>
    <label>City <input v-model="address.city" autocomplete="address-level2" /></label>
    <label>State <input v-model="address.state" autocomplete="address-level1" /></label>
    <label>Country <input v-model="address.country" autocomplete="country-name" /></label>
  </form>
</template>

<style scoped>
.address-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 16px;
  max-width: 560px;
  font-family: system-ui, sans-serif;
}
.address-form > label:first-child,
.address-form > .address-autocomplete {
  grid-column: 1 / -1;
}
.address-form label {
  display: grid;
  gap: 4px;
  font-size: 14px;
}
.address-form input,
.address-form select {
  padding: 8px;
  font-size: 15px;
}
</style>

When the user types 100 Larkin St, the component shows the suggestions returned by the Address Autocomplete API:

A Vue 3 address autocomplete field with the text "100 Larkin St" and five suggestions from the Geoapify Address Autocomplete API: 100 Larkin Street, San Francisco, CA 94102, and four places at the same address, including the San Francisco Public Library Main Library

After the user picks the first suggestion, the handler fills the form:

The Vue 3 address form after selection: the search field shows 100 Larkin Street, San Francisco, CA 94102, United States of America, and the Street, House number, Postcode, City, State, and Country fields are filled with Larkin Street, 100, 94102, San Francisco, California, and United States

The screenshots were rendered from this code with Vue 3.5.46, @geoapify/geocoder-autocomplete 3.1.0, and the minimal.css theme, in a Vite build.

  • Every field stays editable, so the user can correct a value after selection. Text edited after selection is no longer the address the API returned; store place itself, or a flag, if the app needs to know that the address was picked from a suggestion.
  • Small places may have no city, so the handler falls back to town and village.
  • lat and lon are the coordinates of the selected address. Keep them for a map preview, delivery zones, or distance calculations instead of geocoding the same address again.
  • The autocomplete attributes let browsers fill the fields from saved addresses as well.

React to option changes

The library reads options once, when the component mounts, so changing a reactive value later doesn't change the search. To make an option follow Vue state, pass it as its own prop, watch it, and call the matching library method. This example restricts the suggestions to the countries chosen in a select.

Add the prop, the watcher, and a helper to AddressAutocomplete.vue:

import { ref, watch, onMounted, onBeforeUnmount } from "vue";

const props = defineProps({
  apiKey: { type: String, required: true },
  options: { type: Object, default: () => ({}) },
  countryCodes: { type: Array, default: () => [] }
});

onMounted(() => {
  autocomplete = new GeocoderAutocomplete(container.value, props.apiKey, props.options);
  applyCountryFilter(props.countryCodes);
  autocomplete.on("select", (place) => emit("select", place));
});

// Options are read once, so apply later changes with the library's methods
watch(() => props.countryCodes, (codes) => applyCountryFilter(codes));

function applyCountryFilter(codes) {
  if (!autocomplete) return;
  autocomplete.clearFilters();
  if (codes.length) autocomplete.addFilterByCountry(codes);
}

In App.vue, keep the selected countries in a ref and pass them to the component:

<script setup>
import { reactive, ref } from "vue";
// ...
const countryCodes = ref(["us"]);
</script>

<template>
  <form class="address-form" @submit.prevent>
    <label>
      Country
      <select v-model="countryCodes">
        <option :value="[]">Any country</option>
        <option :value="['us']">United States</option>
        <option :value="['ca']">Canada</option>
      </select>
    </label>

    <label>Search address</label>
    <AddressAutocomplete
      :api-key="apiKey"
      :options="autocompleteOptions"
      :country-codes="countryCodes"
      @select="onAddressSelect"
    />
    <!-- address fields as before -->
  </form>
</template>

With United States selected, the library adds filter=countrycode:us to the request, and Paris returns only places in the United States:

The Vue 3 form with the Country select set to United States and "Paris" typed in the address field; the suggestions are Paris in Texas, Kentucky, Tennessee, Maine, and Arkansas

Switching the select to Canada sends filter=countrycode:ca with the next request; Any country removes the filter. clearFilters() removes every filter, so call it before adding the new one. Other options have their own methods, such as setLang(), setLimit(), setType(), addBiasByProximity(), and clearBias(). For the difference between filters and bias, see Restrict address search to a country and Get the right place when names repeat.

Vue address autocomplete with an AI coding agent

Copy this prompt into Codex, Claude Code, Cursor, or another coding agent to add Geoapify address autocomplete to an existing Vue 3 app.

Add address autocomplete to my Vue 3 app with the @geoapify/geocoder-autocomplete library and the Geoapify Address Autocomplete API.

Before you start
- Inspect the existing project first. Reuse its build tool, form components, state management (reactive, Pinia, or a form library), validation, styles, and conventions. Make the smallest necessary changes.
- Ask only what the project doesn't answer: which form gets the autocomplete, which address fields it has, and whether search should be limited to some countries.
- Ask me to configure a Geoapify API key. Read it from an environment variable (VITE_GEOAPIFY_API_KEY in Vite). Never hardcode, print, or log the key, and remind me to restrict it to the site's origins in Geoapify MyProjects.

Implementation
1. npm install @geoapify/geocoder-autocomplete. There is no Vue-specific Geoapify package; wrap the core library.
2. Create an AddressAutocomplete single-file component with <script setup>. Render an empty container div with a ref and position: relative.
3. In onMounted, create new GeocoderAutocomplete(container, apiKey, options). Keep the instance in a plain variable, not a ref.
4. Forward autocomplete.on("select", place => emit("select", place)). place is a GeoJSON feature, or null when the field is cleared.
5. In onBeforeUnmount, call destroy() and drop the instance.
6. Import one theme CSS from @geoapify/geocoder-autocomplete/styles once, at the app level.
7. In the parent, copy place.properties into the form state: street, housenumber, postcode, city (fall back to town, then village), state, country, lat, lon. Ignore a null place.

Requirements
- Options are read once at creation. If an option must follow Vue state, pass it as a separate prop, watch it, and call the library method: clearFilters() then addFilterByCountry(codes), setLang(), setLimit(), setType(), addBiasByProximity(), clearBias().
- Keep every form field editable after selection, and track whether the current values still match the selected place.
- Keep the selected coordinates instead of geocoding the same address again.
- Add an accessible label for the search field and keep browser autocomplete attributes on the address fields.
- Do not create a second instance on re-render; one instance per mounted component.

Review the generated code against this guide. Check that the library is created in onMounted and destroyed in onBeforeUnmount, that the API key comes from the environment, that a null selection doesn't clear the form, and that option changes after mounting go through the library's methods.