Here we are going to use the Geocode api to lookup a place based upon
the name we enter. We'll display the list of results and fire off an
event once the user selects something.
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 |   import Openrouteservice from 'openrouteservice-js'
  import apiKey from './apiKey.js'
  // So we don't do a zillion searches
  export function debounce(func, timeout = 500){
      let timer;
      return (...args) => {
          clearTimeout(timer);
          timer = setTimeout(() => { func.apply(this, args); }, timeout);
      };
  }
  const Geocode = new Openrouteservice.Geocode({
      api_key: apiKey
  })
  class LocationLookup extends HTMLElement {
      connectedCallback() {
          const text = this.getAttribute( 'prompt' ) || "Location Search";
          
          this.insertAdjacentHTML( 'beforeend', `
            <p>${text}</p>
            <form>
              <input
                type="text"
                id="location"
                placeholder="Location"
                class="border-2 rounded"/>
            </form>
            <ul>
            </ul>
  `);
          this.location = this.querySelector( 'input' );
          const processChange = debounce(() => this.lookupLocation());    
          this.location.addEventListener( 'keyup', processChange );
          this.results = this.querySelector( 'ul' );
          this.results.addEventListener( "click", this.fireEvent );
      }
      async lookupLocation() {
          this.results.innerHTML = `<li>Loading up ${location.value}</li>`
        
          const json = await Geocode.geocode( {text: this.location.value} )
          this.results.innerHTML = ""
          for( let i = 0; i < json.features.length; i++ ) {
              const feature = json.features[i];
              this.results.innerHTML +=
                  `<li><a
                         data-lat="${feature.geometry.coordinates[1]}"
                         data-lon="${feature.geometry.coordinates[0]}"
                         class="text-blue-700">${feature.properties.label}</a></li>`
          }
      }
      fireEvent( event ) {
          let location = event.target.dataset;
          const myevent = new CustomEvent("location", {
              bubbles: true,
              detail: location
          });
          this.dispatchEvent( myevent );
      }
  }
  customElements.define( 'location-lookup', LocationLookup )
 | 
We can combine this with the map-view component. We'll add an event
listener and when the location is selected we will update the
attribute of the map, which will set the marker in the right place.
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 |   <html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="location-lookup.js" type="module"></script>
      <script src="map-view.js" type="module"></script>
      <script src="https://cdn.tailwindcss.com"></script>
    </head>
    <body>
      <div class="flex h-screen">
        <div class="w-64 p-4">
          <location-lookup id="from" prompt="Starting Location"></location-lookup>
        </div>
        <div id="map" class="w-full vh">
          <map-view></map-view>
        </div>
      </div>
      <script>
        const ll = document.getElementById( "from" )
        const mv = document.querySelector( "map-view" )
        ll.addEventListener( 'location', (e) => {
          const latlon = `${e.detail.lat},${e.detail.lon}`
          mv.setAttribute( "latlon", latlon );
        } );
        
      </script>
    </body>
  </html>
 | 
Check it: http://localhost:5173/location-lookup.html