Retrieving Autocomplete Predictions

This example executes a query prediction request for the phrase "pizza near Syd" and displays the results as an HTML list.

Read the documentation .

TypeScript

 // This example retrieves autocomplete predictions programmatically from the 
 // autocomplete service, and displays them as an HTML list. 
 // This example requires the Places library. Include the libraries=places 
 // parameter when you first load the API. For example: 
 // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"> 
 function 
  
 initService 
 () 
 : 
  
 void 
  
 { 
  
 const 
  
 displaySuggestions 
  
 = 
  
 function 
  
 ( 
  
 predictions 
 : 
  
 google.maps.places.QueryAutocompletePrediction 
 [] 
  
 | 
  
 null 
 , 
  
 status 
 : 
  
 google.maps.places.PlacesServiceStatus 
  
 ) 
  
 { 
  
 if 
  
 ( 
 status 
  
 != 
  
 google 
 . 
 maps 
 . 
 places 
 . 
 PlacesServiceStatus 
 . 
 OK 
  
 || 
  
 ! 
 predictions 
 ) 
  
 { 
  
 alert 
 ( 
 status 
 ); 
  
 return 
 ; 
  
 } 
  
 predictions 
 . 
 forEach 
 (( 
 prediction 
 ) 
  
 = 
>  
 { 
  
 const 
  
 li 
  
 = 
  
 document 
 . 
 createElement 
 ( 
 "li" 
 ); 
  
 li 
 . 
 appendChild 
 ( 
 document 
 . 
 createTextNode 
 ( 
 prediction 
 . 
 description 
 )); 
  
 ( 
 document 
 . 
 getElementById 
 ( 
 "results" 
 ) 
  
 as 
  
 HTMLUListElement 
 ). 
 appendChild 
 ( 
 li 
 ); 
  
 }); 
  
 }; 
  
 const 
  
 service 
  
 = 
  
 new 
  
 google 
 . 
 maps 
 . 
 places 
 . 
 AutocompleteService 
 (); 
  
 service 
 . 
 getQueryPredictions 
 ({ 
  
 input 
 : 
  
 "pizza near Syd" 
  
 }, 
  
 displaySuggestions 
 ); 
 } 
 declare 
  
 global 
  
 { 
  
 interface 
  
 Window 
  
 { 
  
 initService 
 : 
  
 () 
  
 = 
>  
 void 
 ; 
  
 } 
 } 
 window 
 . 
 initService 
  
 = 
  
 initService 
 ; 
  

JavaScript

 // This example retrieves autocomplete predictions programmatically from the 
 // autocomplete service, and displays them as an HTML list. 
 // This example requires the Places library. Include the libraries=places 
 // parameter when you first load the API. For example: 
 // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"> 
 function 
  
 initService 
 () 
  
 { 
  
 const 
  
 displaySuggestions 
  
 = 
  
 function 
  
 ( 
 predictions 
 , 
  
 status 
 ) 
  
 { 
  
 if 
  
 ( 
 status 
  
 != 
  
 google 
 . 
 maps 
 . 
 places 
 . 
 PlacesServiceStatus 
 . 
 OK 
  
 || 
  
 ! 
 predictions 
 ) 
  
 { 
  
 alert 
 ( 
 status 
 ); 
  
 return 
 ; 
  
 } 
  
 predictions 
 . 
 forEach 
 (( 
 prediction 
 ) 
  
 = 
>  
 { 
  
 const 
  
 li 
  
 = 
  
 document 
 . 
 createElement 
 ( 
 "li" 
 ); 
  
 li 
 . 
 appendChild 
 ( 
 document 
 . 
 createTextNode 
 ( 
 prediction 
 . 
 description 
 )); 
  
 document 
 . 
 getElementById 
 ( 
 "results" 
 ). 
 appendChild 
 ( 
 li 
 ); 
  
 }); 
  
 }; 
  
 const 
  
 service 
  
 = 
  
 new 
  
 google 
 . 
 maps 
 . 
 places 
 . 
 AutocompleteService 
 (); 
  
 service 
 . 
 getQueryPredictions 
 ({ 
  
 input 
 : 
  
 "pizza near Syd" 
  
 }, 
  
 displaySuggestions 
 ); 
 } 
 window 
 . 
 initService 
  
 = 
  
 initService 
 ; 
  

CSS

  

HTML

<html>
  <head>
    <title>Retrieving Autocomplete Predictions</title>

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <p>Query suggestions for 'pizza near Syd':</p>
    <ul id="results"></ul>
    <!-- Replace Powered By Google image src with self hosted image. https://developers.google.com/maps/documentation/places/web-service/policies#other_attribution_requirements -->
    <img
      class="powered-by-google"
      src="https://storage.googleapis.com/geo-devrel-public-buckets/powered_by_google_on_white.png"
      alt="Powered by Google"
    />

    <!-- 
      The `defer` attribute causes the script to execute after the full HTML
      document has been parsed. For non-blocking uses, avoiding race conditions,
      and consistent behavior across browsers, consider loading using Promises. See
      https://developers.google.com/maps/documentation/javascript/load-maps-js-api
      for more information.
      -->
    <script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initService&libraries=places&v=weekly"
      defer
    ></script>
  </body>
</html>  

Try Sample

Clone Sample

Git and Node.js are required to run this sample locally. Follow these instructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.

  
  git 
  
 clone 
  
 - 
 b 
  
 sample 
 - 
 places 
 - 
 queryprediction 
  
 https 
 : 
 //github.com/googlemaps/js-samples.git 
 
  
  cd 
  
 js 
 - 
 samples 
 
  
  npm 
  
 i 
 
  
  npm 
  
 start 
 

Other samples can be tried by switching to any branch beginning with sample- SAMPLE_NAME .

  
  git 
  
 checkout 
  
 sample 
 - 
  SAMPLE_NAME 
 
 
  
  npm 
  
 i 
 
  
  npm 
  
 start 
 
Create a Mobile Website
View Site in Mobile | Classic
Share by: