How can a search bar be added to a map in a Webflow form?

Published on
September 22, 2023

To add a search bar to a map in a Webflow form, you can follow these steps:

  1. Start by creating a new form element in Webflow. Drag and drop a form component onto your page, or select an existing form element to edit.

  2. Inside the form element, add an HTML embed component. This can be found in the Add panel on the left side of the Webflow Designer.

  3. Open the HTML embed component and switch to the Custom Code tab.

  4. In the Custom Code tab, paste the following code snippet:

<input type="text" id="search" placeholder="Search..."><div id="map"></div>
  1. Replace the placeholder text with whatever you want to display as a placeholder in the search bar.

  2. Next, you'll need to write JavaScript code to fetch and render the map using a mapping service like Google Maps or Mapbox. Here's an example using Google Maps:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script><script>  function initMap() {    // Set default map options like center and zoom level    var mapOptions = {      center: { lat: 37.7749, lng: -122.4194 }, // Default location (San Francisco)      zoom: 14 // Default zoom level    };    // Create a new map instance    var map = new google.maps.Map(document.getElementById('map'), mapOptions);    // Add search bar functionality to the map    var input = document.getElementById('search');    var searchBox = new google.maps.places.SearchBox(input);    // Bias the search results towards the current map viewport    map.addListener('bounds_changed', function() {      searchBox.setBounds(map.getBounds());    });    // Listen for the event when a place is selected from the search bar    searchBox.addListener('places_changed', function() {      var places = searchBox.getPlaces();      if (places.length == 0) {        return;      }      // Clear the current markers on the map      markers.forEach(function(marker) {        marker.setMap(null);      });      markers = [];      // For each place, add a marker to the map      var bounds = new google.maps.LatLngBounds();      places.forEach(function(place) {        if (!place.geometry) {          console.log("Returned place contains no geometry");          return;        }        var icon = {          url: place.icon,          size: new google.maps.Size(71, 71),          origin: new google.maps.Point(0, 0),          anchor: new google.maps.Point(17, 34),          scaledSize: new google.maps.Size(25, 25)        };        // Create a marker for each place        var marker = new google.maps.Marker({          map: map,          icon: icon,          title: place.name,          position: place.geometry.location        });        markers.push(marker);        if (place.geometry.viewport) {          // Only geocodes have viewport          bounds.union(place.geometry.viewport);        } else {          bounds.extend(place.geometry.location);        }      });      map.fitBounds(bounds);    });  }</script>
  1. Replace YOUR_API_KEY in the Google Maps script tag with your actual API key. If you don't have a Google Maps API key, you can obtain one by following the instructions in the Google Maps API documentation.

  2. Customize the map options and the default location and zoom level inside the initMap function to fit your needs.

  3. Save your changes and publish your website to see the search bar added to the map in your Webflow form.

Additional questions:

  1. How do I create a form element in Webflow?
  2. What is an HTML embed component in Webflow?
  3. How can I customize the map options using Google Maps API?