Store Locator using GeoJSON Data Source with Custom Marker Bubbles
This guide shows you how to customize store marker bubbles using previously added data source control for your stores data. A GeoJSON file with stores locations and properties is used as an example data source.
The finished example will look like this:
What is needed
- Magic Lane API key token
- Web server (an example is provided)
- GeoJSON file containing the store locations (or use our sample GeoJSON)
Setup
Get your Magic Lane API key token: if you do not have a token, see the Getting Started guide.
This project needs a web server. If you do not have access to a web server, you can easily install a local web server, see the Installing a Local Web Server guide. In this project we use a local web server.
Adding controls
To see how to add GeoJSON data control, store list control and free text search control check out the guides:
- Store Locator using a GeoJSON File as Data Source
- Store Locator using GeoJSON Text String as Data Source
Customizing the marker bubbles
To customize the store marker bubbles you can use the data source control option markerBubbleFunction
for specifying your own style rules for marker hover or click event.
- JavaScript
- HTML
let geojsonDataControl = new gem.control.GeoJsonAddedDataControl("Paris.geojson", "" /*icon default*/, "" /*no icon filter*/,
{
markerBubble: {
markerBubbleClass: 'store-bubble',
markerBubbleFunction: function (elMarker, properties, coords) {
elMarker.innerHTML = '<div class="store-bubble--img-container">' +
'<img class="store-bubble--img" src=' + properties.preview + '></div>' +
'<div class="store-bubble--content">' +
'<div class="store-bubble--title">' + properties['name'] + '</div></div>';
}
},
marker: {
highlightClass: 'highlight-marker-icon'
}
});
defaultAppScreen.addControl(geojsonDataControl);
<style>
.store-bubble {
display: block;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
position: absolute;
width: 240px;
height: 200px;
background: #fff;
border-radius: 7px;
text-decoration: none;
-webkit-font-smoothing: antialiased;
bottom: 120%;
left: 50%;
margin-left: -120px;
font-size: 14px;
display: flex;
flex-direction: column-reverse;
-webkit-box-shadow: 0px 5px 15px rgb(0 0 0 / 15%);
box-shadow: 0px 5px 15px rgb(0 0 0 / 15%);
}
.store-bubble--title {
position: absolute;
text-overflow: ellipsis;
text-align: center;
white-space: nowrap;
overflow: hidden;
bottom: 12px;
width: 100%;
font-weight: 600;
color: #04aa6d;
}
.store-bubble--description {
padding: 17px 10px 7px 10px;
background: #fff;
color: grey;
text-overflow: ellipsis;
text-align: center;
white-space: nowrap;
overflow: hidden;
}
.store-bubble--img-container {
position: absolute;
width: 100%;
height: 80%;
top: 0;
bottom: 0px;
left: 0;
border-radius: 3px 3px 0 0;
overflow: hidden;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.store-bubble--img {
background-position-x: 40px !important;
background-position-y: 5px !important;
position: absolute;
top: 0px;
right: 0;
left: 0px;
background-position-x: 12px;
background-position-y: 15px;
background-size: cover;
width: 100%;
height: 100%;
object-fit: cover;
}
.store-bubble--content {
background: #fff;
height: 20%;
width: 100%;
border-radius: 10px;
overflow: hidden;
}
.highlight-marker-icon {
filter: hue-rotate(70deg) contrast(6.5);
transform: scale(1.2);
}
</style>
Complete example code
- JavaScript
- HTML
// Start by setting your token from https://www.magiclane.com/api/projects
if (gem.core.App.token === undefined) gem.core.App.token = "";
defaultAppScreen = gem.core.App.initAppScreen({
container: "map-canvas"
});
let geojsonDataControl = new gem.control.GeoJsonAddedDataControl("Paris.geojson", "" /*icon default*/, "" /*no icon filter*/,
{
markerBubble: {
markerBubbleClass: 'store-bubble',
markerBubbleFunction: function (elMarker, properties, coords) {
elMarker.innerHTML = '<div class="store-bubble--img-container">' +
'<img class="store-bubble--img" src=' + properties.preview + '></div>' +
'<div class="store-bubble--content">' +
'<div class="store-bubble--title">' + properties['name'] + '</div></div>';
}
},
marker: {
highlightClass: 'highlight-marker-icon'
}
});
let listUIControl = new gem.control.ListControl({
sourceControl: geojsonDataControl,
container: 'menu-list-container',
menuName: 'Store locations example',
titleProperties: ['name'],
detailsProperties: ['kinds'],
imageProperty: ['preview']
});
defaultAppScreen.addControl(geojsonDataControl);
defaultAppScreen.addControl(listUIControl);
let searchControl = new gem.control.SearchControl({
searchPreferences: {
exactMatch: true,
maximumMatches: 3,
setCursorReferencePoint: true
},
highlightOptions: {
contourColor: { r: 0, g: 255, b: 0, a: 255 }
}
});
defaultAppScreen.addControl(searchControl);
<html>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="https://www.magiclane.com/sdk/js/gem.css" />
<title>Store Locator Custom Bubble</title>
<head>
<style>
.store-bubble {
display: block;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
position: absolute;
width: 240px;
height: 200px;
background: #fff;
border-radius: 7px;
text-decoration: none;
-webkit-font-smoothing: antialiased;
bottom: 120%;
left: 50%;
margin-left: -120px;
font-size: 14px;
display: flex;
flex-direction: column-reverse;
-webkit-box-shadow: 0px 5px 15px rgb(0 0 0 / 15%);
box-shadow: 0px 5px 15px rgb(0 0 0 / 15%);
}
.store-bubble--title {
position: absolute;
text-overflow: ellipsis;
text-align: center;
white-space: nowrap;
overflow: hidden;
bottom: 12px;
width: 100%;
font-weight: 600;
color: #04aa6d;
}
.store-bubble--description {
padding: 17px 10px 7px 10px;
background: #fff;
color: grey;
text-overflow: ellipsis;
text-align: center;
white-space: nowrap;
overflow: hidden;
}
.store-bubble--img-container {
position: absolute;
width: 100%;
height: 80%;
top: 0;
bottom: 0px;
left: 0;
border-radius: 3px 3px 0 0;
overflow: hidden;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.store-bubble--img {
background-position-x: 40px !important;
background-position-y: 5px !important;
position: absolute;
top: 0px;
right: 0;
left: 0px;
background-position-x: 12px;
background-position-y: 15px;
background-size: cover;
width: 100%;
height: 100%;
object-fit: cover;
}
.store-bubble--content {
background: #fff;
height: 20%;
width: 100%;
border-radius: 10px;
overflow: hidden;
}
.highlight-marker-icon {
filter: hue-rotate(70deg) contrast(6.5);
transform: scale(1.2);
}
</style>
</head>
<body>
<div id="store-locator" style="width: 100%; height: 100%">
<div id="menu-list-container" style="width: 30%; height: 100%; position: absolute"></div>
<div id="map-canvas" style="width: 70%; height: 100%; position: absolute; left: 30%"></div>
</div>
<script type="text/javascript" src="https://www.magiclane.com/sdk/js/gemapi.js"></script>
<script type="text/javascript" src="token.js"></script>
<script type="text/javascript" src="jsStoreLocatorCustomBubble.js"></script>
</body>
</html>
Files
The finished example consists of the project directory containing these files:
- JavaScript code (
.js
file extension) - HTML code (
.html
file extension) - Store locations in GeoJSON format (
.geojson
file extension)
To run the example, the HTML file is loaded in a browser.
Source code for this example:
Right-click on the links and select Save As.
Related
See Store Locator using Studio Defined Data Source for an example with GeoJSON data uploaded to the map studio, and rendered on the map.
See Store Locator using SQL Server as Data Source for an example with GeoJSON data queried dynamically using SQL from a server, and rendered on the map.
See Store Locator using GeoJSON File as Data Source with Custom Controls for an example with GeoJSON data loaded from a file, with Map+List.
See Store Locator using GeoJSON Text String as Data Source for an example with GeoJSON data defined in a variable in JavaScript, and rendered on the map.
JavaScript Examples
Maps SDK for JavaScript Examples can be downloaded or cloned with Git