npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-google-maps-address-autocomplete

v1.0.16

Published

Google Maps Address Autocomplete suggests street addresses to users as they type an address into a form. This component aims at integrating this logic seamlessly into your Vue.js project.

Downloads

466

Readme

Googlemaps address autocomplete (Vue.js)

Description:

Vue.js package that helps you to seamlessly integrate the "auto suggest" and "autocomplete" feature of Google Maps' API.
Google API has developped a nice auto-suggests feature that will help users as they type an address into a form. This component is build on top of that Google feature and will thus:

  • auto-suggest the available address from Google Maps
  • prepopulate the address fields as the user select an address in the list

Demo


Usage

1. Register on Google console

Google Maps Platform to get an API key from Google.

2. npm install this package

https://www.npmjs.com/package/vue-google-maps-address-autocomplete

npm i vue-google-maps-address-autocomplete

OR

yarn add vue-google-maps-address-autocomplete

3. Define your $googleApiKey ! Required !

// Vue 2
Vue.prototype.$googleMapsApiKey = 'yourApiKey';

// Vue 3
Vue.config.globalProperties.$googleMapsApiKey = 'yourApiKey';

4. Register globally or locally this component.

Globally in main.js

import AddressAutocomplete from 'vue-google-maps-address-autocomplete';
Vue.use(AddressAutocomplete);

Locally in a component.vue

import AddressAutocomplete from 'vue-google-maps-address-autocomplete';
export default {
  ...,
  components: { addressAutocomplete },
  ...
};

5. Component interface:

The feature is implemented through a wrapper component: <address-autocomplete>slot</address-autocomplete>.
The component will expose a slot giving you the possibility to structure and style your component anyway you want. This package does not provide any design implementation as this is purely a renderless component that will just provide some JS logic to implement the autosuggest & autocomplete functionalities.

Example of usage

<template>
  <address-autocomplete
    v-slot="{ addressAutocompleteRef, loadGoogleMapsScript }"
    @update-address="populateAutocompletedAddress"
  >
    // Here comes your code
    <div>
      <input
        v-model="address.streetName"
        :ref="addressAutocompleteRef"
        @input.once="loadGoogleMapsScript"
      >
      <input v-model="address.streetNumber">
      <input v-model="address.streetBox">
      <input v-model="address.zipCode">
      <input v-model="address.city">
    </div>
  </address-autocomplete>
</template>

<script>
  export default {
    data () {
      return {
        address: { streetName, streetNumber, zipCode, city }
      }
    },
    ...,
    methods: {
      populateAutocompletedAddress (autocompletedAddress) {
        // Here you assign the autocompletedAddress to your address data.
        this.address = { ...this.address, ...autocompletedAddress };
      }
    }
  }
</script>

Properties & Events:

1. Ref [REQUIRED]
It exposes the addressAutocompleteRef attribute. This attribute is required on your "auto suggest" input with a :ref binding. This is the input where the client will start typing and the suggestions from GoogleMaps will show up.

<input v-model="address.streetName" :ref="addressAutocompleteRef">

2. Props
This provides the option to restrict the suggestions to specific countries. Props must be passed as a two-character, ISO 3166-1 Alpha-2 compatible country code (i.e. "br", "sg", "fr") in an array.

  • props: countryRestrictions
  • Default is: ["be"]
  • Type: Array
<address-autocomplete
  v-slot="{ addressAutocompleteRef, loadGoogleMapsScript }"
  @update-ddress="populateAutocompletedAddress"
  :country-restrictions="['au', 'nz']"
>
  <slot></slot>
</address-autocomplete>

3. Methods
Exposes a loadGoogleMapsScript [REQUIRED] method to asynchronously load the GoogleMaps script . This way, you're in control of when the script is actually loaded (ie: when user touches the input or @mounted).

<input v-model="address.streetName" @input.once="loadGoogleMapsScript">

4. Hook
It exposes the @update-address hook that is triggered when the user select an option in the available list, passing the autocompletedAddress. This will prepopulated the address fields:

  • streetName
  • streetNumber
  • zipCode
  • city
methods: {
  populateAutocompletedAddress (autocompletedAddress) {
    this.address = { ...this.address, ...autocompletedAddress };
  }
}

Use with care and enjoy 😉