The advantages of embedding maps in WordPress with free software

Who hasn’t embedded a map on their website or a client’s at some point? In this post we’ll look at why it’s important to choose free software as an alternative when embedding maps in WordPress. This post is inspired by the talk I gave in March 2023 at WordCamp Chiclana, which you can watch below…

destacada mapas en wordpress open source

Who hasn’t embedded a map on their website or a client’s at some point? In this post we’ll look at why it’s important to choose free software as an alternative when embedding maps in WordPress.

This post is inspired by the talk I gave in March 2023 at WordCamp Chiclana, which you can watch below (in Spanish):

The problem

We’ve all added a map to our websites or our clients’ at some point. The typical business location map or the “Where to find us” map. And we always turn to Google Maps to embed it.

Remember the days when you saw loads of maps with the “For development purposes only” notice and the background half hidden behind a grey veil?

Google Maps error image: "For development purposes only"
The dreaded message saying the Google map isn’t loading correctly.

This was caused by a sudden change to the Google Maps API, which made it less “free” to use: it became necessary to sign up for the API and, above all, enter your card details in case one day you had enough traffic to start paying a modest price for using the famous mapping platform. That said, on this blog we explained a little trick for embedding a map without using the Google Maps API.

However, I was sure Google had to be taking a toll on our websites’ performance, given its fondness for using every single one of its services to collect information about each user click.

This raises several problems:

  • If the map makes too many external requests or connections, our website may show poor performance, with the consequences that can have for SEO.
  • On the other hand, user behavior on the website can be tracked through the map. This, especially in Europe, is a privacy problem. Nor does it look good if the map doesn’t work when the user doesn’t accept the cookies it needs to work properly.

Options for embedding a map on a website

At first glance, we could consider several solutions:

  • We could not add the map to the website: I don’t think we need to go to such extremes. It’s true that it solves every possible problem except one: how does the user find our business?
  • We could embed a Google map in an iframe. This is the solution we want to avoid, and the one that causes these problems. Let’s keep looking.
  • We could take a screenshot of the map. As you can see, I think it’s a shoddy solution and I don’t like it at all. It also raises some accessibility issues. That’s why I’ve crossed it out.
  • We could use a map plugin. Now that seems more civilized. Which plugin could it be? Let’s look at some examples.
    • Map Block for Google Maps: A fairly interesting plugin for a Google map. It makes connecting to the API easier and works with blocks, which also makes it easier to use on the site.
    • WP Open Street Map: An interesting plugin, since it uses a base map other than Google’s, in this case OpenStreetMap. It’s open source.
    • Leaflet Map: Another open-source technology, based on a JavaScript mapping library.
    • Mapster WP Maps: Now we’re talking big league. It’s a very interesting commercial plugin that uses Mapbox, a very powerful JavaScript mapping library.

Why can a map make a website slow?

Let’s look at what’s called a waterfall chart. In this case, we created it with the performance analysis tool WebPageTest, which lets us run a speed and performance test on any website:

waterfall chart 1
Waterfall chart

As you can see, this chart shows all the requests a website makes from the moment we type in the domain name, or click a Google search result, until the page has fully loaded.

If you look closely, from request 17 to the last one, number 227, the word Google appears. So more than 200 requests are being made to Google over 23 seconds of loading. It’s true that you can interact with the website before those 23 seconds are up, but I’d have left already.

jimmy fallon

Let’s settle it with an experiment

After seeing this, I decided to run an experiment. I created 6 identical WordPress sites. These sites had the same features:

  • A classic theme (Twenty Twelve)
  • 10 posts shown in full on the home page. Yes, some websites may need to show several maps on the same page, so I wanted to see the cumulative effect they could have on its performance.
  • On every site except one, which I used as a control, I embedded 10 maps, one in each post, with different locations so they wouldn’t be cached.
  • I ran 5 speed tests on each site with WebPageTest to get a representative sample and an average measurement. I calculated this average using the median rather than the mean, to remove the skewing effect of some anomalous measurements.

From the tests I extracted the following metrics, which I considered the most important for answering my questions:

  • FCP (First Contentful Paint): The time from when the website is requested until the first pixel is painted on screen.
  • DC Time (Document Complete Time): The time until everything needed for us to interact with the website’s elements has loaded, while other things keep loading.
  • Time: The website’s total load time. When the browser’s spinner stops, indicating there’s nothing left to load.
  • Requests: All the external requests the website makes in order to show all its content.
  • Total bytes: The size, in kilobytes, of the files downloaded to our browser to show the complete website.

I gathered all this in a spreadsheet and calculated the averages for each of the chosen metrics (please don’t leave, these are the only numbers you’ll see).

Experiment data in its spreadsheet
Data used in the experiment

Results of the experiment

If we look at the results for the first two metrics, First Contentful Paint and DC Time, we see that there are no significant differences.

Charts showing the FCP and DC Time metrics for the performance experiment
Results of the experiment for First Contentful Paint and DC Time

Keep in mind that, in these and the following charts, the first column is the control site, the second is the site where I added Google maps with an iframe (no API), and the third is a site where Google Maps maps are added with a plugin using the API. Of the rest, the fourth is a plugin that embeds maps using OpenStreetMap, the fifth is a plugin that works with Leaflet and the last is the plugin that uses Mapbox. These are the plugins I mentioned at the start of the post.

Notice that the first pixel is painted with differences of tenths of a second, while the moment we can interact is basically the same, with very little difference. It would take a photo finish.

So this tells us absolutely nothing. But what about the other metrics? Let’s look at the results.

The rest of the experiment's charts
The rest of the experiment’s charts, with total load time, total downloaded size in KB and number of requests.

Well, it can be said now: the twin towers in every chart are Google.

As you can see, the control site did its job, giving significantly lower values for the site’s total load time, the amount of data downloaded and the number of requests, which are minimal.

However, we see that both total load time and the number of requests are truly outrageous, both when embedding a map with an iframe and when doing it through a plugin connected to the Google API.

It’s worth highlighting how little time both OpenStreetMap and Leaflet take to load, as well as the low number of requests made by the three non-Google options.

Without a doubt, the worst option is embedding an iframe. Embedding one website inside another is a bit old-fashioned; it’s a technology of the past.

Mapbox does take longer to load in total, getting close to Google, and that’s understandable, since it offers graphic possibilities that Google doesn’t. In fact, we’ll treat Mapbox somewhat separately, since it’s a JavaScript library with a 3D rendering engine that lets us do amazing things.

Which solution would I choose for embedding a map?

I think that, in light of these results, there’s no doubt that Leaflet, because of the possibilities it offers and its performance, seems to be the most suitable option for embedding a map on a website.

In fact, if you’re a WordPress developer, I encourage you to dive into Leaflet and to create new plugins that use this library. If you don’t know what Leaflet is, you can learn about it in this post.

How can we add Leaflet to WordPress?

We can do it thanks to several plugins:

  • Map Block Leaflet: A fantastic and very simple plugin, which I wrote about on this blog. It’s great for adding simple maps.
  • Leaflet Map: You could say it’s the official Leaflet plugin. It’s quite complete, although it’s a bit behind the times in that it still works with shortcodes. Even so, it’s a very good option for somewhat more complex maps.
  • Extensions for Leaflet Map: An add-on to the previous plugin that lets you do some pretty wild things. It’s somewhat experimental, but you can’t miss it if you want more powerful maps. For example, you could build cycling route maps from your GPS’s .gpx file. But you can do much more.

Conclusions

As we’ve seen in this post, free software offers very powerful options for adding maps to a WordPress website without compromising its performance.

Judging by the data, Google Maps offers rather little, at a fairly high cost in performance and privacy.

So a very interesting option may be Leaflet, if our needs are basic to intermediate. And if we need more power, with 3D maps showing certain effects and so on, Mapbox will always be a very good option.

Tell me about your experience in the comments, and whether you know of other free-software plugins for embedding maps.

Jesús Yesares Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *