Skip to main content
SDKs & dev tools 12 min read

Adding Maps to Your Site

Adding Maps to Your Site

A few weeks ago I showed live on Twitch how to add maps and map data to an existing ecommerce site I created as a demo. My thinking was to create a real-world situation (a company with stores) that wanted to use maps to provide a better experience to their users. In the stream, I went through a couple of different iterations to create better and better results. Let's get started!

Let's begin by looking at our ecommerce site. Before I begin, note that all of the code I'm sharing below can all be found on my GitHub repository at

The Cat and Sasquatch Boutique ( is a well-known shop providing grooming and other services to felines and members of the Bigfoot family (Sasquatch, Abominable Snowman, Yeti, and so forth). Located in southern Louisiana, they've been providing services to their community for years now but have been working to improve their online presence to help bring in new customers. None of the links actually work, but you can click on Locations to see a static list of shops and their locations:

The HTML behind this is a simple unordered list:

<li>2133 Kaliste Saloom Rd, Lafayette, LA 70508</li>
<li>301 Verot School Rd, Lafayette, LA 70508</li>
<li>4311 Johnston St, Lafayette, LA 70503</li>
<li>1812 W Pinhook Rd, Lafayette, LA 70508</li>
<li>600 E Kaliste Saloom Rd, Lafayette, LA 70508</li>

This "works" in terms of the bare minimum. If I live in the area, I'll probably know those streets and know how to get there. But I have to be honest. I got these addresses from a list of local gas stations. I know each of these streets. I've lived here for twenty years. But I'd still check a map before I went to one of them. How can we make this better for our visitors? For a first attempt, and one that can be done very quickly, let's use the Map Image API. This is less of an API and more of a super-powered image service. By simply crafting the right URL and putting it in an <img> tag, you get custom maps to use in your HTML.

The URL can load a map based on a precise location, or via searching by passing in geographic values. We have a street, city, and zip, and in order to render them, we can construct a URL like so:

Copied{KEY}&n={STREET NUMBER}&s={STREET}&ci={CITY}&zi={ZIP}

So, given our first location of "2133 Kaliste Saloom Rd, Lafayette, LA 70508", our URL could look like so (notice I've also specified a height and width):


And then to use it, literally just put that in an img tag:

        <img src="{KEY}&n=2133&s=Kaliste+Saloom+Rd&ci=Lafayette&zi=70508&w=300&h=300">

You will want to replace {KEY} with your key, and in case you're wondering, yes, it is safe to include the keys in your HTML. In the HERE Developer Portal, you can add domain restrictions to your keys so that can only be used on certain domains:

To update our rather boring list of locations we can simply add the images to our list:

<img src="">
2133 Kaliste Saloom Rd, Lafayette, LA 70508

<img src="">
301 Verot School Rd, Lafayette, LA 70508

<img src="">
4311 Johnston St, Lafayette, LA 70503

<img src="">
1812 W Pinhook Rd, Lafayette, LA 70508

<img src="">
600 E Kaliste Saloom Rd, Lafayette, LA 70508

Here's the result. Note that I could absolutely lay this out a bit nicer!

You can try this online at

Alright, that's cool and all, but what if we want a more dynamic map? I mean, one that would allow the user to pan and zoom so they could plan out a route to the store. For that, we need HERE's JavaScript Maps library. Adding a map to our HTML page is relatively straightforward. First, we include the required libraries:

        <script src="" type="text/javascript" charset="utf-8"></script>
<script src="" type="text/javascript" charset="utf-8"></script>
<script src="" type="text/javascript" charset="utf-8"></script>
<script src="" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="" />

Next, I removed my previous hard coded list of locations and included an empty div for the map:

        <div id="mapContainer"></div>

I used a bit of CSS to make the container sized in a nice manner:

#mapContainer {
    width: 100%;
    height: 700px;

Now we need to use some JavaScript. We'll begin by adding the map:

        const KEY = 'c1LJuR0Bl2y02PefaQ2d8PvPnBKEN8KdhAOFYR_Bgmw';
var platform = new H.service.Platform({
    'apikey': KEY

// Obtain the default map types from the platform object:
var defaultLayers = platform.createDefaultLayers();

var map = new H.Map(
        zoom: 12,
        center: { lat: 30.22, lng: -92.02 },
        pixelRatio: window.devicePixelRatio || 1

var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));

    // Create the default UI:
var ui = H.ui.UI.createDefault(map, defaultLayers);

This is fairly boilerplate except for the center value. As my stores are in the Lafayette, LA region I've focused the map there.

Now we need to add markers for our stores. Currently we only have addresses for our stores, not longitude and latitude values. The JavaScript Maps library support our Geocoding APIs but I chose to instead use HERE WeGo. It lets you search for an address and get longitude and latitude information.

With those values, the API to add a marker is incredibly simple. Here's all the locations:

        map.addObject(new{lat:30.164709, lng:-92.039268}));
map.addObject(new{lat:30.174879, lng:-92.00705}));
map.addObject(new{lat:30.194481, lng:-92.057503}));
map.addObject(new{lat:30.19084, lng:-92.016212}));
map.addObject(new{lat:30.19569, lng:-92.00473}));

And the result:

Now you can pan around, zoom in, and so forth, to get a better idea of the store's location. You can demo this yourself here: There still leaves a lot to be desired though. The map has a problem I've seen on many web sites. While you can zoom in and see a precise location of a store, we've lost the street names. Consider this location:

Is that on Rena Drive or Johnston Street? Also, what if our stores had different opening hours, or contact information? It would be nice to be able to get this information, perhaps on clicking those markers? Luckily the HERE Maps library provides an excellent way of doing that - Infobubbles. An infobubble is a small bubble (like speech in a comic book) that you can populate with HTML. Let's look at an updated version of our code that set the markers.

First, we'll create a group. Groups in our Maps library let you associate UI items together that share some common code. You'll see how this is used later:

        var group = new;

Now let's make our markers. Previous our markers were just that - a marker on the map. Now we're going to associate some data with them. Markers can have any data associated with them. I chose to use HTML that included the street address as well as hours.

        let marker = new{lat:30.164709, lng:-92.039268});
<p class='infoBubble'>
<b>Location:</b> 2133 Kaliste Saloom Rd, Lafayette, LA 70508<br/>
<b>Hours:</b> 8AM-10PM


Notice that I also added it to my group. I repeated this for all 4 locations and then simply added the group itself to the map:


Ok, so the last bit is adding support for showing the infobubble. Luckily the API is really simple to use. Since all our markers are in one group, we can add an event listener to it directly like so:

        group.addEventListener('tap', evt => {
    // event target is the marker itself, group is a parent event target
    // for all objects that it contains
    var bubble =  new H.ui.InfoBubble(, {
        // read custom data
    // show info bubble
}, false);

When anything in the group is tapped, a new infobubble is opened and the content from the markers data is used to render the content. And that's literally it. Now when you click, you'll see information about the location:

You can play around with this yourself at

Let's recap. We began with a bulleted list of street addresses for our locations. Next, we changed them into small static maps showing the location in context of other streets in the city. The third version switched to a dynamic map with markers, but no other information was visible. The fourth version added the ability to click on a marker to see an infobubble containing the store address as well as it's open hours. For our final version, I'm really going to kick it up a notch. Let me share a picture of what we're building and then I'll explain how it was done.

What you see above is the map figuring out where I am, calculating the routes to different stores based on current conditions, and then rendering a route to the 'best' location for me if I were to hop in the car with my sasquatch for a good grooming. So how was this done? I wrote one main function to handle this process.

        async function getRoutes() {
    let myLocation = await getLocation();
    let lowest = Number.POSITIVE_INFINITY;
    let chosenRoute;

    // draw routes from me to all locations

    for(l of locations) {
        let result = await getRoute({lat:myLocation.latitude, lng:myLocation.longitude}, l.position);
        let totalTime = result.routes[0].sections[0].travelSummary.duration;
        if(totalTime < lowest) {
            lowest = totalTime;
            chosenRoute = result.routes[0];

    if(chosenRoute) {
        document.querySelector('#routeContainer').innerHTML = `
        <b>New Feature!</b> The closest location (based on time to arrive) is now highlighted on the map. The home icon represents your location.

The function begins by firing off a request to getLocation, which uses the browsers geolocation API to get my position. Once it has it, it can then fire off multiple requests to our routing API. Here's getRoute:

        async function getRoute(from, to) {
    var routingParameters = {
        'routingMode': 'fast',
        'transportMode': 'car',
        // The start point of the route:
        'origin': `${},${from.lng}`,
        // The end point of the route:
        'destination': `${},${to.lng}`,
        // Include the route shape in the response
        'return': 'polyline,travelSummary'

    return new Promise((resolve, reject) => {
        router.calculateRoute(routingParameters, r => resolve(r), e => reject(e));

Each call to getRoute returns route data that includes the duration of the route. By looping over the results, I can then figure out the quickest one. I update the HTML and call drawRouteOnMap to render the route data returned by the call. Here's that function:

        function drawRouteOnMap(route) {
    route.sections.forEach((section) => {
        // Create a linestring to use as a point source for the route line
        let linestring = H.geo.LineString.fromFlexiblePolyline(section.polyline);

        // Create a polyline to display the route:
        let routeLine = new, {
            style: { strokeColor: 'blue', lineWidth: 3 }

        var icon = new'img/home.png');

        // Create a marker for the start point:
        let startMarker = new, { icon:icon});

        // Add the route polyline and the two markers to the map:
        map.addObjects([routeLine, startMarker]);


The combination of the routing result data and the power JavaScript library makes this all rather simple. Note that this approach wouldn't be sensible if you had a large number of stores. With a few locations though it runs quick enough to be near instantaneous. You can try this yourself but be prepared for slightly longer routes. Our code could actually handle using a "sanity check" where if your location was more than one or two hundred miles away the routing isn't displayed. Here's the demo:

I hope you found this walkthrough interesting. The entire repo can be found here: Don't forget to sign up for a free developer account and reach out to us on Twitter if you have any questions about this code.

Raymond Camden

Raymond Camden

Have your say

Sign up for our newsletter

Why sign up:

  • Latest offers and discounts
  • Tailored content delivered weekly
  • Exclusive events
  • One click to unsubscribe