All Projects → Gapur → google-place-autocomplete

Gapur / google-place-autocomplete

Licence: other
🏆 Best practice with Google Place Autocomplete API on React

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to google-place-autocomplete

crud-app
❄️ A simple and beautiful CRUD application built with React.
Stars: ✭ 61 (-10.29%)
Mutual labels:  create-react-app, react-hooks
react-antd-admin
基于vite2.x + react17.x + typescript4.x + antd4.x + react-router6.x + mobx6.x编写的一款简单高效的前后端分离的权限管理系统
Stars: ✭ 140 (+105.88%)
Mutual labels:  create-react-app, react-hooks
forex-web-app
💱 foreign currency exchange app built with react hooks
Stars: ✭ 17 (-75%)
Mutual labels:  create-react-app, react-hooks
epee-react-admin
🗡简洁、高效、易扩展的 React 快速开发模板,基于布局设计,纯 Hooks 开发,包含中后台应用常用功能
Stars: ✭ 87 (+27.94%)
Mutual labels:  create-react-app, react-hooks
movies
🍿 react-app for movies
Stars: ✭ 60 (-11.76%)
Mutual labels:  create-react-app, react-hooks
react-guidebook
📚 React 知识图谱 关于概念、技巧、生态、前沿、源码核心
Stars: ✭ 22 (-67.65%)
Mutual labels:  react-hooks
flhooks
React like Hooks implementation for Flutter.
Stars: ✭ 38 (-44.12%)
Mutual labels:  react-hooks
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (-58.82%)
Mutual labels:  react-hooks
public-ol-web-template
OrangeLoops Web Project Boilerplate
Stars: ✭ 28 (-58.82%)
Mutual labels:  create-react-app
crassa
Create React App Server Side Application
Stars: ✭ 16 (-76.47%)
Mutual labels:  create-react-app
cra-and-storybook
Stars: ✭ 13 (-80.88%)
Mutual labels:  create-react-app
cra-bundle-analyzer
Use Webpack Bundle Analyzer on a create-react-app application without ejecting
Stars: ✭ 46 (-32.35%)
Mutual labels:  create-react-app
mobx-nextjs-root-store
Mobx root store implementation for next.js with server-side rendering
Stars: ✭ 42 (-38.24%)
Mutual labels:  react-hooks
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+261.76%)
Mutual labels:  react-hooks
cra-macro-example
This is an example of how you can use Create React App with Babel Plugin Macros.
Stars: ✭ 38 (-44.12%)
Mutual labels:  create-react-app
react-use-storage
React Hook to handle local and session storage
Stars: ✭ 18 (-73.53%)
Mutual labels:  react-hooks
react-intl.macro
Extract react-intl messages with babel-plugin-macros.
Stars: ✭ 39 (-42.65%)
Mutual labels:  create-react-app
react-with-threejs-example
An example project integrating React with three.js
Stars: ✭ 27 (-60.29%)
Mutual labels:  react-hooks
style-hook
use css in js with react hook.
Stars: ✭ 16 (-76.47%)
Mutual labels:  react-hooks
react-firebase-template
Bootstrap a React + Firebase full stack application with every thing you need pre-configured: hosting, database, authentication, CI, Typescript, Material UI, PWA and other goodies.
Stars: ✭ 24 (-64.71%)
Mutual labels:  create-react-app

Google Place Autocomplete

The best practice with Google Place Autocomplete API on React

Using Google Place Autocomplete without third-party library

Autocomplete is a feature of the Places library in the Maps JavaScript API. When a user starts typing an address, autocomplete fills in the rest.

Setting up the Project

Install the repository:

git clone https://github.com/Gapur/google-place-autocomplete.git

After that, move it into the google-place-autocomplete directory and run it from the terminal:

cd google-place-autocomplete
npm start

Before we get started, you need the API-Key for the Google Places API. You can get that key here. I store Google API Key in the .env file — you should too.

Magic Code

Let’s implement the main SearchLocationInput component to work with the Google Place Autocomplete API. First, we have to create a function to load the script for working with Google API. Let’s create SearchLocationInput.js with loadScript function:

// dynamically load JavaScript files in our html with callback when finished
const loadScript = (url, callback) => {
  let script = document.createElement("script"); // create script tag
  script.type = "text/javascript";

  // when script state is ready and loaded or complete we will call callback
  if (script.readyState) {
    script.onreadystatechange = function() {
      if (script.readyState === "loaded" || script.readyState === "complete") {
        script.onreadystatechange = null;
        callback();
      }
    };
  } else {
    script.onload = () => callback();
  }

  script.src = url; // load by url
  document.getElementsByTagName("head")[0].appendChild(script); // append to head
};

I used this script for dynamic JavaScript for fast page speed load on our public pages. It dynamically loads JavaScript files with a callback when finished. Next, we have to assign the Google Place Map to the autoComplete variable when the component is rendered:

// handle when the script is loaded we will assign autoCompleteRef with google maps place autocomplete
function handleScriptLoad(updateQuery, autoCompleteRef) {
  // assign autoComplete with Google maps place one time
  autoComplete = new window.google.maps.places.Autocomplete(
    autoCompleteRef.current,
    { types: ["(cities)"], componentRestrictions: { country: "us" } }
  );
  autoComplete.setFields(["address_components", "formatted_address"]); // specify what properties we will get from API
  // add a listener to handle when the place is selected
  autoComplete.addListener("place_changed", () =>
    handlePlaceSelect(updateQuery)
  );
}

Then we attach to the autocomplete listener, which listens for whenever a user selects one of the autocomplete suggestions. Let’s createthe handlePlaceSelect method to handle selection:

async function handlePlaceSelect(updateQuery) {
  const addressObject = autoComplete.getPlace(); // get place from google api
  const query = addressObject.formatted_address;
  updateQuery(query);
  console.log(addressObject);
}

This method is called when the event is triggered and gets place data from API. Then we can do any operation with data.

Google API result

Article on Medium

How to Use Google Place Autocomplete With React Without a Third-Party Library

How to contribute?

  1. Fork this repo
  2. Clone your fork
  3. Code 🤓
  4. Test your changes
  5. Submit a PR!
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].