Store Locator using GeoJSON Data Source with Custom List Control
This guide shows you how to customize a previously added list control for your stores data source. 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 or Store Locator using GeoJSON Text String as Data Source.
Customizing the list control
To customize your stores list you can use the list control options:
cssClasses
for specifying your own style rules for the list container with the options:divMenu
for specifying menu container style rules,listHeader
for specifying the menu name style,divListCountCss
for specifying the items count style,divList
for specifying the style rules for the scrollable items list and the divItem option for specifying style for the item cards.populateItemFunction
for customizing your store cards layout.
- JavaScript
- HTML
let listUIControl = new gem.control.ListControl({
sourceControl: geojsonDataControl,
container: 'menu-list-container',
menuName: 'Store locations example',
titleProperties: ['name'], // geojson property key selected for usage as title
detailsProperties: ['kinds'],
cssClasses: {
divMenu: {
className: 'list-menu',
type: 'div'
},
listHeader: {
className: 'list-name',
type: 'div'
},
divListCountCss: {
className: 'list-count',
type: 'div'
},
divList: {
className: 'list-group',
type: 'div'
},
divItem: {
className: 'card',
type: 'div'
}
},
populateItemFunction: function (divItemObj, jsonProperties) {
divItemObj.innerHTML = '<img class="card-img-top" src="' + jsonProperties.preview + '"/>' +
'<div class="card-body">' +
'<h5 class="card-title">' + jsonProperties.name + '</h5>' +
'<p class="card-text">' + jsonProperties.kinds + '</p> </div>'
}
});
<style>
.list-menu {
background-color: #f8f8f8;
overflow: hidden;
position: absolute;
height: 100%;
}
.list-name {
padding: 0.4rem;
overflow-y: auto;
word-break: break-word;
-webkit-overflow-scrolling: touch;
}
.list-name h2 {
font-size: 1.2rem;
margin-bottom: 0.1rem;
}
.list-count {
text-align: center;
}
.list-group {
max-height: 100%;
overflow-y: auto;
overflow-x: hidden;
scrollbar-width: thin;
word-break: break-word;
-webkit-overflow-scrolling: touch;
}
.card {
flex-direction: row;
cursor: pointer;
font-size: 0.7rem;
padding: 0.2rem;
}
.card h5 {
font-size: 0.8rem;
}
.card img {
width: 35%;
cursor: pointer;
object-fit: cover;
padding-top: 0.4rem;
padding-bottom: 0.4rem;
padding-left: 0.2rem;
}
.card-body {
padding: 0.7rem;
}
.card:hover {
background-color: gold;
}
.card.active {
background-color: #ceede1;
}
</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: {
title: ['name'],
image: ['preview']
}
});
let listUIControl = new gem.control.ListControl({
sourceControl: geojsonDataControl,
container: 'menu-list-container',
menuName: 'Store locations example',
titleProperties: ['name'], // geojson property key selected for usage as title
detailsProperties: ['kinds'],
cssClasses: {
divMenu: {
className: 'list-menu',
type: 'div'
},
listHeader: {
className: 'list-name',
type: 'div'
},
divListCountCss: {
className: 'list-count',
type: 'div'
},
divList: {
className: 'list-group',
type: 'div'
},
divItem: {
className: 'card',
type: 'div'
}
},
populateItemFunction: function (divItemObj, jsonProperties) {
divItemObj.innerHTML = '<img class="card-img-top" src="' + jsonProperties.preview + '"/>' +
'<div class="card-body">' +
'<h5 class="card-title">' + jsonProperties.name + '</h5>' +
'<p class="card-text">' + jsonProperties.kinds + '</p> </div>'
}
});
defaultAppScreen.addControl(geojsonDataControl);
defaultAppScreen.addControl(listUIControl);
let searchControl = new gem.control.SearchControl({
highlightOptions: {
contourColor: { r: 0, g: 255, b: 0, a: 0 }
},
searchPreferences: {
exactMatch: true,
maximumMatches: 3,
addressSearch: true,
mapPoisSearch: true,
setCursorReferencePoint: true
}
});
defaultAppScreen.addControl(searchControl);
<html>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="https://www.magiclane.com/sdk/js/gem.css" />
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous"
/>
<title>Store Locator Geojson Controls</title>
<head>
<style>
.list-menu {
background-color: #f8f8f8;
overflow: hidden;
position: absolute;
height: 100%;
}
.list-name {
padding: 0.4rem;
overflow-y: auto;
word-break: break-word;
-webkit-overflow-scrolling: touch;
}
.list-name h2 {
font-size: 1.2rem;
margin-bottom: 0.1rem;
}
.list-count {
text-align: center;
}
.list-group {
max-height: 100%;
overflow-y: auto;
overflow-x: hidden;
scrollbar-width: thin;
word-break: break-word;
-webkit-overflow-scrolling: touch;
}
.card {
flex-direction: row;
cursor: pointer;
font-size: 0.7rem;
padding: 0.2rem;
}
.card h5 {
font-size: 0.8rem;
}
.card img {
width: 35%;
cursor: pointer;
object-fit: cover;
padding-top: 0.4rem;
padding-bottom: 0.4rem;
padding-left: 0.2rem;
}
.card-body {
padding: 0.7rem;
}
.card:hover {
background-color: gold;
}
.card.active {
background-color: #ceede1;
}
</style>
<script
src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"
></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"
></script>
</head>
<body>
<div id="store-locator" style="width: 100%; height: 100%">
<div id="menu-list-container" style="width: 35%; height: 100%; position: absolute"></div>
<div id="map-canvas" style="width: 65%; height: 100%; position: absolute; left: 35%"></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="jsStoreLocatorCustomList.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 custom UI controls.
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.