Info Window

See how to display content in an overlay that looks like a popup window and how to configure and style it using Templating and CSS.

  1. Create an Info Window
  2. Build a TemplateRenderer
  3. Open an Info Window
  4. Close an InfoWindow

An Info Window displays properties of a store in a bubble above the map, at the store latlng position and usually opened upon map events, such as user clicks. The Woosmap JS Store Locator API offers you two ways to build an Info Window:

If you create a new woosmap.LocatorWindow specifying any infoBoxOptions it will implement a woosmap.ui.InfoBox.

Here is a sample demonstrating the use of a standard woosmap.LocatorWindow with template rendering.

Create an Info Window

To add an Info Window, you’ll need to create a new instance of woosmap.LocatorWindow and passing it a woosmap.TemplateRenderer object to specify the HTML structure in which the store attributes will be rendered.

JavaScript
        const templateInfoWindow =
        "<div class='store-title'>{{name}}</div>" +
        "<div class='store-address'>{{address.lines}}</div>" +
        "<div class='store-contact'><a href='tel:{{contact.phone}}'>{{contact.phone}}</a></div>";
const renderer = new woosmap.TemplateRenderer(templateInfoWindow);
const infoWindow = new woosmap.LocatorWindow(map, renderer);

    

You don’t need to create multiple instances of LocatorWindow for different store markers.

Build a TemplateRenderer

The Woosmap JS Store Locator API embeds a simple logic-less template system like Mustache. The woosmap.TemplateRenderer that you put as parameter to the woosmap.LocatorWindow will receive a store properties object. Therefore, the tags specified in the template should correspond to the properties object of Store Data Structure.

Simple Template

Here’s a simple example.

JavaScript
        const templateInfoWindow =
        "<div class='store-title'>{{name}}</div>" +
        "<div class='store-address'>{{address.lines}}</div>" +
        "<div class='store-contact'><a href='tel:{{contact.phone}}'>{{contact.phone}}</a></div>";

    

Assume that the selected store have this following properties.

JSON
        {
  "store_id": "123456",
  "name": "SuperMarket Edinburgh",
  "address": {
    "lines": ["20 Castle Terrace"]
  },
  "contact": {
    "phone": "+441313375757"
  }
}

    

The rendered template will look like.

HTML
        <div class='store-title'>SuperMarket Edinburgh</div>
<div class='store-address'>20 Castle Terrace</div>
<div class='store-contact'><a href='tel:+441313375757'>+441313375757</a></div>

    

Register Lambda Function

As the template system is logic-less it could be hard to display advanced behaviour. To enable a finer-grained processing of your stores attributes you can add to your template some custom tags/functions using the woosmap.TemplateRenderer.registerLambda method. You will have access to the selected store properties inside the lambda function using infoWindow.get("selectedStore").

Here is a sample to put an opening label when the store is opened.

JavaScript
        function getOpeningLabel() {
  let openLabel = "Close";
  const store = infoWindow.get("selectedStore").properties;
  if (store.open && store.open.open_now) {
    openLabel = store.open.current_slice["all-day"]
      ? "24/24"
      : store.open.current_slice.start + "" + store.open.current_slice.end;
    openLabel =
      store.open && store.open.open_now ? "Open now: " + openLabel : "";
  }
  return openLabel;
}

const templateInfoWindow =
        "<div class='store-title'>{{name}}</div>" +
        "<div>{{#opening_label}}{{/opening_label}}</div>";

const renderer = new woosmap.TemplateRenderer(templateInfoWindow);
renderer.registerLambda('opening_label', getOpeningLabel);

    

Open an Info Window

The woosmap.TiledView handles the click event on all store markers and save the store clicked into a property called selectedStore. As the woosmap.LocatorWindow and the woosmap.TiledView both extend the MVCObject this property is bindable. Therefore, to automatically open the Info Window when a user select a store on the Map, bind the selectedStore property to the LocatorWindow.

JavaScript
        mapView.bindTo("selectedStore", infoWindow);

    

The following example displays coffee shops at the center of London. When the user clicks the marker, an info window opens.

JavaScript
        let map, mapView, infoWindow;
const templateInfoWindow =
        "<div class='store-title'>{{name}}</div>" +
        "<div class='store-address'>{{address.lines}}</div>" +
        "{{#contact.phone}}<div class='store-contact'><a href='tel:{{contact.phone}}'>{{contact.phone}}</a></div>{{/contact.phone}}";

function initMap() {
  const loader = new window.woosmap.MapsLoader(googleLoadOptions);
  loader.load(function () {
    map = new window.google.maps.Map(
      document.getElementById("map"),
      googleMapsOptions
    );
    mapView = new window.woosmap.TiledView(map, woosmapOptions);
    const renderer = new woosmap.TemplateRenderer(templateInfoWindow);
    const infoWindow = new woosmap.LocatorWindow(map, renderer);
    mapView.bindTo("selectedStore", infoWindow);
  });
}

    

You can also decide to open the infoWindow on a given store using openOn() method.

JavaScript
        const dataSource = new woosmap.DataSource();
dataSource.getStoreById("2329", (store) => {
  infoWindow.openOn(store);
});

    

Close an InfoWindow

Once opened, an InfoWindow can be closed either when the user clicks the close button at top right or by explicitly calling the close() method. For example, you can close the InfoWindow when the user clicks outside it on the map.

JavaScript
        map.addListener('click', (e) => {
  infoWindow.close();
})

    
Was this article helpful?
Have more questions? Submit a request