jQuery(document).ready(function($) { //jQuery('#gmaps-js').remove(); function initializeAutocomplete() { var input = document.querySelector('.custom-search'); const ncBounds = new google.maps.LatLngBounds( new google.maps.LatLng(33.7, -84.3), // Southwestern corner of NC new google.maps.LatLng(36.6, -75.4) // Northeastern corner of NC ); var options = { bounds: ncBounds, strictBounds: true, // Strictly restrict results to the defined bounds componentRestrictions: { country: 'us' }, // Restrict to the United States //componentRestrictions: { country: 'us' ,administrativeArea:'NC' }, // Restrict to the United States fields: ["address_components", "formatted_address", "geometry", "name", "place_id"] //types: ['address'] // Restrict to addresses only }; var autocomplete = new google.maps.places.Autocomplete(input, options); // Listen for the 'place_changed' event autocomplete.addListener('place_changed', function() { var place = autocomplete.getPlace(); //console.log(place); if (place.address_components) { // Extract the state and check if it's NC var state = place.address_components.find(function(component) { return component.types.includes("administrative_area_level_1"); }).short_name; var street_number = place.address_components.find(function(component) { return component.types.includes("street_number"); })?.long_name || ""; var subpremise = place.address_components.find(function(component) { return component.types.includes("subpremise"); })?.long_name || ""; var neighborhood = place.address_components.find(function(component) { return component.types.includes("neighborhood"); })?.long_name || ""; var route = place.address_components.find(function(component) { return component.types.includes("route"); })?.long_name || ""; var city = place.address_components.find(function(component) { return component.types.includes("locality"); })?.long_name || ""; var county = place.address_components.find(function(component) { return component.types.includes("administrative_area_level_2"); })?.long_name || ""; var postal_code = place.address_components.find(function(component) { return component.types.includes("postal_code"); })?.long_name || ""; // If the state is not NC, ignore the result if (state !== 'NC') { alert('Please select an address in North Carolina (NC).'); return; } //console.log(place.address_components); //console.log(input.value); // Remove the country from the formatted address manually var formatted_address = place.formatted_address.replace(/, USA$/, ''); var place_id = place.place_id; var addressObject = { subpremise: subpremise, neighborhood: neighborhood, street_number: street_number, route: route, city: city, county: county, postal_code: postal_code }; // Converting the array to a JSON string var jsonString = JSON.stringify(addressObject); // Sending the JSON string through the URL var encodedURL = encodeURIComponent(jsonString); //console.log(jsonString); //console.log(encodedURL); //console.info('/properties-search-results/' + '?rets_search=' + encodeURIComponent(input.value) +'&rets_search_json='+encodedURL); // Construct the URL with the place ID as a query parameter // Redirect the user to the new URL when they select a place window.location.href = '/properties-search-results/' + '?rets_search=' + encodeURIComponent(input.value) +'&rets_search_json='+encodedURL; } }); // Manually handle the rendering of autocomplete results google.maps.event.addListener(autocomplete, 'place_changed', function () { var place = autocomplete.getPlace(); // Further actions if needed... }); } google.maps.event.addDomListener(window, 'load', initializeAutocomplete); });