1// [START maps_places_autocomplete_addressform] 2// This sample uses the Autocomplete widget to help the user select a 3// place, then it retrieves the address components associated with that 4// place, and then it populates the form fields with those details. 5// This sample requires the Places library. Include the libraries=places 6// parameter when you first load the API. For example: 7// <script 8// src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"> 9let placeSearch; 10let autocomplete; 11const componentForm = { 12 street_number: "short_name", 13 route: "long_name", 14 locality: "long_name", 15 administrative_area_level_1: "short_name", 16 country: "US", 17 postal_code: "short_name", 18}; 19 20function initAutocomplete() { 21 // Create the autocomplete object, restricting the search predictions to 22 // geographical location types. 23 autocomplete = new google.maps.places.Autocomplete( 24 document.getElementById("autocomplete_location"), 25 { types: ["geocode"] } 26 ); 27 // Avoid paying for data that you don't need by restricting the set of 28 // place fields that are returned to just the address components. 29 autocomplete.setFields(["address_component"]); 30 // When the user selects an address from the drop-down, populate the 31 // address fields in the form. 32 autocomplete.addListener("place_changed", fillInAddress); 33 34 autocomplete.setComponentRestrictions({ 35 country: [current_site_slug], 36 }); 37 38} 39 40// [START maps_places_autocomplete_addressform_fillform] 41function fillInAddress() { 42 // Get the place details from the autocomplete object. 43 const place = autocomplete.getPlace(); 44 45 for (const component in componentForm) { 46 document.getElementById(component).value = ""; 47 document.getElementById(component).disabled = false; 48 } 49 50 // Get each component of the address from the place details, 51 // and then fill-in the corresponding field on the form. 52 for (const component of place.address_components) { 53 const addressType = component.types[0]; 54 55 if (componentForm[addressType]) { 56 const val = component[componentForm[addressType]]; 57 document.getElementById(addressType).value = val; 58 } 59 } 60} 61 62// [END maps_places_autocomplete_addressform_fillform] 63// [START maps_places_autocomplete_addressform_geolocation] 64// Bias the autocomplete object to the user's geographical location, 65// as supplied by the browser's 'navigator.geolocation' object. 66function geolocate() { 67 if (navigator.geolocation) { 68 navigator.geolocation.getCurrentPosition((position) => { 69 const geolocation = { 70 lat: position.coords.latitude, 71 lng: position.coords.longitude, 72 }; 73 const circle = new google.maps.Circle({ 74 center: geolocation, 75 radius: position.coords.accuracy, 76 }); 77 autocomplete.setBounds(circle.getBounds()); 78 }); 79 } 80} 81// [END maps_places_autocomplete_addressform_geolocation] 82// [END maps_places_autocomplete_addressform]
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.