Using a custom location search service with the Searchbar

This example demonstrates the use of a custom Location search service with the Searchbar.

Try searching for 'Alcatraz', 'Bridge' or 'Pier' and select on of the location options.

<!DOCTYPE HTML>
<html>
  <head>
    <script src="https://unpkg.com/wrld.js@1.x.x"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.1/leaflet.css" rel="stylesheet" />

    <link href="https://cdn-webgl.wrld3d.com/wrldjs/addons/resources/v1/latest/css/wrld.css" rel="stylesheet"/>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://cdn-webgl.wrld3d.com/wrldjs/addons/searchbar/v1/latest/searchbar.js"></script>
  </head>
  
  <body>
  <div style="position: relative">
    <div id="widget-container" class="wrld-widget-container"></div>
    <div id="map" style="height: 400px"></div>
    <script>
      var map = Wrld.map("map", "your_api_key_here", {
        center: [37.794, -122.41],
        zoom: 15
      });

      var locations = [
        {title: "Golden Gate Bridge", subtitle:"37.818109, -122.478202", location: {latLng: Wrld.latLng(37.818109, -122.478202), zoom: 15, headingDegrees: 60}},
        {title: "Oakland Bay Bridge", subtitle:"37.799121, -122.377214", location: {latLng: Wrld.latLng(37.799121, -122.377214), zoom: 15, headingDegrees: -30}},
        {title: "Bay Bridge", subtitle:"37.819869, -122.345328", location: {latLng: Wrld.latLng(37.819869, -122.345328), zoom: 15, headingDegrees: -5}},
        {title: "Pier 39", location: { latLng: Wrld.latLng(37.809698, -122.410383), zoom: 17, headingDegrees: 235}},
        {title: "Alcatraz Island", location: { latLng: Wrld.latLng(37.826771, -122.422921), zoom: 16, headingDegrees: 260}}
      ];

      var calculateDistSqr = function(latLng1, latLng2) {
        return Math.pow(latLng1.lat - latLng2.lat, 2) + Math.pow(latLng1.lng - latLng2.lng, 2);
      };

      var locationSearchService = {
        fetchNearbyLocationsByTerm: function(latLng, term, callback) {
          term = term.trim().toLowerCase();
          var results = [];
          if (term.length > 0) {
            locations.forEach(function(location) {
              var termPos = location.title.toLowerCase().indexOf(term);
              if (termPos !== -1) {
                results.push(Object.assign({}, location, { distSqr: calculateDistSqr(latLng, location.location.latLng) }));
              }
            });
          }
          results.sort(function(lhs, rhs) { return lhs.distSqr - rhs.distSqr; });
          callback(results);
        },

        fetchAutocompleteOptions: function(latLng, term, callback) {
          term = term.trim().toLowerCase();
          var options = [];
          if (term.length > 0) {
            locations.forEach(function(location) {
              var termPos = location.title.toLowerCase().indexOf(term);
              if (termPos !== -1) {
                options.push(Object.assign({}, location, { pos: termPos }));
              }
            });
          }
          options.sort(function(lhs, rhs) { return lhs.pos - rhs.pos; });
          callback(options);
        }
      };

      var searchbarConfig = {
        locationSearchService: locationSearchService
      };

      var searchbar = new WrldSearchbar("widget-container", map, searchbarConfig);
    </script>

  </div>
  </body>
</html>
v1.1.0