Querying Location Collection and Calculating Midpoint — Multiple Markers on Google Maps with Wix…
I use a backend function to retrieve our locations and calculate a midpoint. All done in javascript and integrated with Wix Velo.
I use a backend function to retrieve our locations and calculate a midpoint. All done in javascript and integrated with Wix Velo.
Welcome back to my series on displaying multiple locations on Google Maps with Wix Velo. I’m excited and grateful that you’ve chosen to have a look into what I’ve learned, and I hope that you find this tutorial helpful.
We’re on our second episode of our five part series, and I hope you’re finding it valuable!
Our goal in this series is to build a website which fully integrates the display of multiple, dynamic map data points into a Google Maps display. We want to be able to update a location and have it almost immediately display on our website. If you’d like to read more about why this is important, here’s my introduction.
Working Example: The example we’ll be using is shown here.
Github / Code Repo: For those who find it useful, the public github repo is here.
Along the way, I’ll share couple of tips on some security considerations, along with helpful observations which will hopefully save you many hours of troubleshooting. The full list of episodes can be found at the bottom of each episode.
As always, I hope this content is helpful. It means the absolute world to me when you take time to clap for an episode, subscribe to my stories or use my referral link to sign up for Medium. It lets me know that my stories are helping you ❤️.
Enjoy, and feel free to drop me a DM on twitter, connect on LinkedIn / Github with thoughts and comments 😃
We’ll be covering the initial query and processing of our location information. Here’s the link to the full code for this episode.
We’ll be covering the following:
.jsw
file to contain our backend functionalityLet’s get started!
In all modern web development, there is a differentiation between frontend and backend development. Both are pivotal for how websites work, and there is a whole bunch of discussion about what they contribute to the overall user experience.
When it comes to this project, we’ll be doing as much of our processing on the backend as possible, for the following reasons:
Here’s how we do this:
queryLastTenLocations.jsw
You should now have your backend file set up. Nice work!
Wix has some awesome features which make it wonderful for web development. Here’s two which are relevant to our project:
node.js
This means that almost all of our code (except for HTML Elements ironically) can be written in javascript, which really reduces our development time.The combination of these two elements is really going to help us out.
Wix makes extensive use of asynchronous functions (here’s a great post if you want to learn about this). These are denoted by the keywords await
and async
. As a general rule of thumb, all Wix Data queries are asynchronous. This means that if we need to perform sequential (otherwise known as synchronous) operations on the data we’ve asked for, we need to wait for ( i.e. await
) the data return before we move forward.
If you don’t do this, you’ll end with an error along the lines of promise unresolved
or you’ll discover that your code will tell you it was unable to perform operation on empty array
.
If you come across these errors during your development, take a quick look at your promises chains — you may find the error without too much heartache.
With that covered, let’s get going!
import wixData from 'wix-data';
async function getLastTenEntries(){}
Using the details about your collection from Episode 1, fill in the following details:
Here’s the code:
It turns out there’s a variety of methods to calculate midpoints when it comes to map locations. Without diving too deeply into debate, you can think of the discussion in several ways:
If it’s a topic which interests you, here’s some interesting links: here, here and a great set of libraries if you need to be super accurate.
For our purposes, I’m using a method which is accurate enough to centre our map across the 10 coordinates. It’s reasonably simple, reasonably accurate and doesn’t require a huge amount of knowledge about coordinates.
The process we go through to calculate it is as follows:
(As a special note, this method also allows us to weight our locations if we wanted to. It’s not relevant for this project, but may be helpful for you somewhere else).
Here’s the code to do all of this:
Our final step is to combine our 10 locations together with our midpoint into a single JSON object. We’ll also need to specify our target, a concept explained further in episode 4. This is the object we will present to the frontend portion of the site.
We also need to identify the target to which our data is aimed. This will become more relevant in tutorial xxx when we start filling out our HTML Element.
The steps are:
getLastTenEntries()
function to query the dataset for the last 10 locationscalculateMidpoint(locations)
function to calculate their midpointHere’s the code:
Let’s test this function to make sure it works.
Make a note of the format of the data.
And that’s a wrap for Episode 2! We’ve covered a lot!
In our next episode, we’ll be diving into our HTML Element and getting it ready to receive our locations!
The full list of episodes in this series are linked below to help you navigate quickly ❤