By Evan Futterman

Are you ready to take the plunge into some hard-hitting HubL code and JavaScript to build a robust location finder? Then you have found the perfect post. In a previous post, we showed you how to create dynamic pages using HubDB, but now we are going to dig a little deeper into some more advanced features and walk through the intricacies of putting all of these pieces together.

Although HubDB is not a true relational database, you can get much more functionality out of it than you would expect. The only main downside to HubDB is trying to reorder entries made into the table, but luckily HubSpot gives developers some pretty decent documentation on how to change the ordering once rendered on the actual page.

Prepare the Database

This tutorial assumes that you have a working knowledge of HubL and JQuery/JavaScript. For the purpose of this tutorial, I keep the logic as simple as possible, giving you the ability to expand upon it as necessary. I show live screenshots of how this is implemented on a real site, as well as the use cases associated with it.

The first step is to create a brand new HubDB table. You’ll want to add any necessary attributes, such as the location name, hours, address, or any other items you find relevant. The most important item to add is a location column, which is a built-in GPS coordinate column.

hubdb1

Register Google Maps API & Set Up Store Locator Plugin

Google Maps recently changed its process for using the Google Maps API. It now requires that an account be set up with a credit card attached in case you reach the limit for API calls. Once you successfully set up your account, you will receive a unique API key.

You will need to add the following script sources to your site replacing the key below with your unique key. You will also need to include JavaScript source files for the jQuery Store Locator Plugin, as well as the handlebars.js plugin.


<script src="https://maps.google.com/maps/api/js?key=908734lhsd7y9"></script>


Next, you’ll need to prepare your HTML to have data inserted into it. You’ll also set up a form input so that the user can enter an address or zip code that will be checked against the jQuery Store Locator Plugin.


<div class="section no-padding-top">
 <div class="page-center">
   <div class="bh-sl-container">
     <div class="bh-sl-form-container">
       <form id="bh-sl-user-location" method="get" action="#">
         <label for="address">Find a location close to:</label>
         
         <div class="form-input">
           <input type="text" id="address" name="address" placeholder="" />
           <button id="bh-sl-submit" type="submit" class="bh-sl-button">Submit</button>
         </div>
       </form>
     </div>

     <div id="bh-sl-map-container" class="bh-sl-map-container">
       <div id="bh-sl-map" class="bh-sl-map"></div>
       <div class="bh-sl-loc-list">
         <ul class="list"></ul>
       </div>
     </div>
   </div>
 </div>
</div>


Now is time for the jQuery Store Locator Plugin. In the mapSettings call, you can see how we reference the Google Maps API, and in the dataRaw field, we are plugging in all of the HubDB fields for each row. You can edit the various properties by using the documentation for the Store Locator Plugin. For instance, you may want to set a custom marker image that fits your branding or changes basic colors and stylings.


<script>
$(function() {
   $('#bh-sl-map-container').storeLocator({
      dataType: 'json',
       dataRaw: [],
    infowindowTemplateID: 'infowindowTemplate',
    listTemplateID: 'listTemplate',
    mapSettings: { zoom : 0, mapTypeId: google.maps.MapTypeId.ROADMAP },
    maxDistance: false,
       querystringParams: true,
    fullMapStart: true,
    storeLimit: -1,
    disableAlphaMarkers: true,
       listColor1: '#ffffff',
       listColor2: '#ffffff',
    markerImg: 'https://app.hubspot.com/hubfs/_img/our-story/locations/marker.png',
       selectedMarkerImg: 'https://app.hubspot.com/hubfs/_img/our-story/locations/marker-active.png',
       markerDim: { height: 63, width: 53 },
       selectedMarkerImgDim: { height: 63, width: 53 },
       addressID: 'address'
   });
});
</script>

 
Everything you need to plan, build, and launch a stunning website can be found  in "Mastering The Art of Website Redesigns".    

Showing the Results

In order to show the results, you need to have an HTML section that will be revealed after the search has been completed and that you can reference in the jQuery. The results will be revealed and utilize the Handlebars plugin to push the data to the screen in an easy and effective manner.

For the sake of this example, we have tried to use the simple as naming conventions possible so that you can understand what is happening behind the scenes.

You can even create conditions, as seen with the location “if” statement, to only render data that is relevant to the search result so that there is no extraneous HTML. For a developer, it doesn’t get much better than that!


 <div id="listTemplate">
 
   {{#location}}
   <li data-markerid="{{markerid}}">
    <div class="list-details">
    <div class="list-content">
         <h2>{{name}}</h2>
         <p class="loc-address">{{address}}</p>
         <div class="loc-meta">
           <div class="loc-phone">
             <span class="loc-phone-icon"></span>
             <a href="tel:{{phone}}">{{phone}}</a>
           </div>
           <div class="loc-lobby">
             <p>Lobby Hours</p>
             {{{lobby_hours}}}
           </div>
           <div class="loc-drivethru">
             <p>Drive-Thru Hours</p>
             {{{drive_thru_hours}}}
           </div>
         </div>

         {{#if distance}}
           <div class="loc-dist"><strong>Distance:</strong> {{distance}} {{length}}</div>
           <div class="loc-directions"><a href="https://maps.google.com/maps?saddr={{origin}}&amp;daddr={{street_address}} {{city}}, {{state}} {{zip}}" target="_blank" title="{{address}}">Get Directions</a></div>
         {{/if}}
    </div>
    </div>
   </li>
   {{/location}}
 
 </div>

 

You may notice that the “raw” section is referenced both in the jQuery Store Locator Plugin as well as in the HTML above. In the Store Locator Plugin, we are assigning variables using Handlebars that we are later revealing their content for each individual search result.

This is all you need to create a simple store locator. You will notice that the Locator Plugin will return an error message via a browser alert if a search query doesn’t return a result from within your specified range of miles.

How Does It Look?

map2

office

As you can see, once you are able to get all of the information to render on the page, you have the ability to add any CSS and stylings that you might need. If you run into any errors, I suggest manually plugging numbers into the Store Locator Plugin to assess whether the error is from HubDB or your initial setup with the jQuery Store Locator Plugin.

Although there are many ways to leverage HubDB in your website redesign and many more advanced options for this locator, hopefully this information will get you started on your HubDB journey to creating dynamic content for your visitors.

Mastering-The-Art-of-Website-Redesigns-cover

Plan, design, build, and launch your website successfully with:

Mastering The Art of Website Redesigns

Check It Out
Evan Futterman

About the author

Evan Futterman is a front-end developer with experience in internet and digital marketing. With a Business degree in Computer Information Systems, Evan has both the technical and business knowledge to deliver the quality SmartBug Media clients expect. When not doing web development he can usually be found outside exploring the outdoors. Read more articles by Evan Futterman.

Subscribe to get our new blogs delivered right to your inbox