This is destructive FORK (i.e. it fundamentally alters the construction but not functionality) of Shadcn Address Autocomplete to include the following:
- Replacement of API Routes with Server Actions
- Addition of Nearby Functionality/Addition of geolocation option to group nearest search results first
- Addition of session tokens in google places/autocomplete calls in order to reduce cost and bill appropriately
- Addition of Google Logo/powered by to meet licensing conditions
- Addition of a shadcn toast to advise that location is going to be used to narrow results (where authorised).
- Removal of duplicate/null calls to API routes
- Note: Linting removed due to not intending this to be a maintained fork.
- Re-organisation of code base to provide a component that can be re-used
- Mocks removed because not relevant to my need.
The core functionality remains the same as build by NiazMorshed2007 who authored the component.
You just get a new box that lets a user share their location, google then groups the locations closest to that point in a circle. (bias, not restriction)
An address autocomplete component built with Google Places API (new) and shadcn/ui components.
Screen.Recording.2024-07-09.at.8.11.08.AM.mov
Note 📝 The live url uses mock data, you should take care of setting api keys properly for real uses.
- Framework: Next.js
- Styling: Tailwind CSS
- UI Components: shadcn/ui
- Validation: Zod
-
Clone the repository
git clone https://github.com/NiazMorshed2007/shadcn-address-autocomplete
-
Install dependencies using pnpm
pnpm install
-
Copy the
.env.exampleto.envand update the variables.cp .env.example .env
**For this fork, you need to create a .env with the google_places_api_key
-
Start the development server
pnpm run dev
The API Routes are included and updated to provide the same functionality as the server actions, but are deprecated and not used.
- The _components directory contains all the custom components and actions, including their types.
- This can be imported into a project and then adjusted to needs.
- You need to change the imports to your implementation of the shadcn UI components (the components/ui directory)- or import these standard components into your UI location.
- You do not need the page.tsx in the root or the theme provider. These can be removed if you use alternative.
- To get the google logo to display, you need to put the google logo somewhere accessible - there are copies in public/images that you could use.
- You need to create a .env file specifying your GOOGLE_PLACES_API_KEY=
- The Google_Places_API_Key only requires the Google Places (New) API - you can disable all others.
- Make sure you set restrictions on your Google API key, it uses a Server Action so you should be able to restrict it to just your domain (when hosted)
- Add an import to the component
- Add the component tag