{"id":19076148,"url":"https://github.com/gapur/google-place-autocomplete","last_synced_at":"2025-04-30T01:54:12.801Z","repository":{"id":39239024,"uuid":"237804702","full_name":"Gapur/google-place-autocomplete","owner":"Gapur","description":"🏆 Best practice with Google Place Autocomplete API on React","archived":false,"fork":false,"pushed_at":"2023-04-03T21:49:38.000Z","size":1672,"stargazers_count":73,"open_issues_count":8,"forks_count":28,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-30T01:54:06.653Z","etag":null,"topics":["create-react-app","google-place-api","google-place-autocomplete","react","react-hooks"],"latest_commit_sha":null,"homepage":"https://medium.com/@gapur.kassym/the-best-practice-with-google-place-autocomplete-api-on-react-939211e8b4ce","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gapur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-02T17:02:11.000Z","updated_at":"2024-04-02T17:40:28.000Z","dependencies_parsed_at":"2025-04-18T08:32:06.162Z","dependency_job_id":"5c995800-f8b8-49ff-9587-fb829dee6cb4","html_url":"https://github.com/Gapur/google-place-autocomplete","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gapur%2Fgoogle-place-autocomplete","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gapur%2Fgoogle-place-autocomplete/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gapur%2Fgoogle-place-autocomplete/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gapur%2Fgoogle-place-autocomplete/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gapur","download_url":"https://codeload.github.com/Gapur/google-place-autocomplete/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251626888,"owners_count":21617741,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["create-react-app","google-place-api","google-place-autocomplete","react","react-hooks"],"created_at":"2024-11-09T01:57:18.150Z","updated_at":"2025-04-30T01:54:12.752Z","avatar_url":"https://github.com/Gapur.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg width=\"700\"src=\"https://github.com/Gapur/google-place-autocomplete/blob/master/public/example.gif\"\u003e\n\u003c/p\u003e\n\n# Google Place Autocomplete\n\nThe best practice with Google Place Autocomplete API on React\n\nUsing Google Place Autocomplete without third-party library\n\nAutocomplete is a feature of the Places library in the Maps JavaScript API. When a user starts typing an address, autocomplete fills in the rest.\n\n## Setting up the Project\n\nInstall the repository:\n```sh\ngit clone https://github.com/Gapur/google-place-autocomplete.git\n```\n\nAfter that, move it into the google-place-autocomplete directory and run it from the terminal:\n```\ncd google-place-autocomplete\nnpm start\n```\n\nBefore we get started, you need the API-Key for the Google Places API. You can get that key [here](https://developers.google.com/maps/documentation/javascript/places-autocomplete). I store Google API Key in the .env file — you should too.\n\n## Magic Code\n\nLet’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:\n\n```js\n// dynamically load JavaScript files in our html with callback when finished\nconst loadScript = (url, callback) =\u003e {\n  let script = document.createElement(\"script\"); // create script tag\n  script.type = \"text/javascript\";\n\n  // when script state is ready and loaded or complete we will call callback\n  if (script.readyState) {\n    script.onreadystatechange = function() {\n      if (script.readyState === \"loaded\" || script.readyState === \"complete\") {\n        script.onreadystatechange = null;\n        callback();\n      }\n    };\n  } else {\n    script.onload = () =\u003e callback();\n  }\n\n  script.src = url; // load by url\n  document.getElementsByTagName(\"head\")[0].appendChild(script); // append to head\n};\n```\n\nI 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:\n\n```js\n// handle when the script is loaded we will assign autoCompleteRef with google maps place autocomplete\nfunction handleScriptLoad(updateQuery, autoCompleteRef) {\n  // assign autoComplete with Google maps place one time\n  autoComplete = new window.google.maps.places.Autocomplete(\n    autoCompleteRef.current,\n    { types: [\"(cities)\"], componentRestrictions: { country: \"us\" } }\n  );\n  autoComplete.setFields([\"address_components\", \"formatted_address\"]); // specify what properties we will get from API\n  // add a listener to handle when the place is selected\n  autoComplete.addListener(\"place_changed\", () =\u003e\n    handlePlaceSelect(updateQuery)\n  );\n}\n```\n\nThen 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:\n\n```js\nasync function handlePlaceSelect(updateQuery) {\n  const addressObject = autoComplete.getPlace(); // get place from google api\n  const query = addressObject.formatted_address;\n  updateQuery(query);\n  console.log(addressObject);\n}\n```\n\nThis method is called when the event is triggered and gets place data from API. Then we can do any operation with data.\n\n## Google API result\n\n\u003cp align=\"center\"\u003e\n  \u003cimg width=\"800\"src=\"https://github.com/Gapur/google-place-autocomplete/blob/master/public/result.png\"\u003e\n\u003c/p\u003e\n\n## Article on Medium\n\n[How to Use Google Place Autocomplete With React Without a Third-Party Library](https://medium.com/better-programming/the-best-practice-with-google-place-autocomplete-api-on-react-939211e8b4ce)\n\n## How to contribute?\n\n1. Fork this repo\n2. Clone your fork\n3. Code 🤓\n4. Test your changes\n5. Submit a PR!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgapur%2Fgoogle-place-autocomplete","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgapur%2Fgoogle-place-autocomplete","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgapur%2Fgoogle-place-autocomplete/lists"}