Store Locator using GeoJSON Data Source with Custom Icons
This guide shows you how to add different icons for your store data based on a GeoJSON property.
The finished example will look like this:
When to use
- The entire store location data set is relatively small and can be easily downloaded.
- The data set is not changing frequently.
- The data set contains different categories of items.
What is needed
- Magic Lane API key token
- Web server (an example is provided)
- SVG files of the different store categories (or use our sample SVGs)
- 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.
Adding custom icons
- JavaScript
- HTML
// Start by setting your token from https://www.magiclane.com/api/projects
if (gem.core.App.token === undefined) gem.core.App.token = "";
let svgLogo = "./store.svg";
defaultAppScreen = gem.core.App.initAppScreen({
container: "map-canvas"
});
let iconFilter = [{ key: 'kinds', value: 'supermarkets', iconClass: 'supermarket-icon' },
{ key: 'kinds', value: 'conveniences', iconClass: 'supermarket-icon' },
{ key: 'kinds', value: 'bakeries', iconClass: 'bakery-icon' }];
let geojsonDataControl = new gem.control.GeoJsonAddedDataControl("Paris_shops.geojson", svgLogo, iconFilter,
{
markerBubble: {
title: ['name'],
image: ['preview']
}
});
defaultAppScreen.addControl(geojsonDataControl);
<html>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="https://www.magiclane.com/sdk/js/gem.css">
<title>Store Locator GeoJSON Data Custom Icons</title>
<head>
<style>
.supermarket-icon {
background-image: url(./supermarket.svg);
background-position: center;
background-repeat: no-repeat;
background-size: contain;
width: 25px !important;
height: 25px !important;
position: absolute;
margin: 0px auto;
cursor: pointer;
}
.supermarket-icon:hover {
width: 30px !important;
height: 30px !important;
}
.bakery-icon {
background-image: url(./bakery.svg);
background-position: center;
background-repeat: no-repeat;
background-size: contain;
width: 25px !important;
height: 25px !important;
position: absolute;
margin: 0px auto;
cursor: pointer;
}
.bakery-icon:hover {
width: 30px !important;
height: 30px !important;
}
</style>
</head>
<body>
<div id="store-locator" style="width: 100%; height: 100%">
<div id="map-canvas" style="width: 100%; height: 100%; position: absolute;">
</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="jsStoreGeojsonCustomIcons01.js"></script>
</body>
</html>
Complete code example
- JavaScript
- HTML
// Start by setting your token from https://www.magiclane.com/api/projects
if (gem.core.App.token === undefined) gem.core.App.token = "";
let svgLogo = "./store.svg";
defaultAppScreen = gem.core.App.initAppScreen({
container: "map-canvas"
});
let iconFilter = [{ key: 'kinds', value: 'supermarkets', iconClass: 'supermarket-icon' },
{ key: 'kinds', value: 'conveniences', iconClass: 'supermarket-icon' },
{ key: 'kinds', value: 'bakeries', iconClass: 'bakery-icon' }];
let geojsonDataControl = new gem.control.GeoJsonAddedDataControl("Paris_shops.geojson", svgLogo, iconFilter,
{
markerBubble: {
title: ['name'],
image: ['preview']
}
});
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({
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">
<title>Store Locator GeoJSON Data Custom Icons</title>
<head>
<style>
.supermarket-icon {
background-image: url(./supermarket.svg);
background-position: center;
background-repeat: no-repeat;
background-size: contain;
width: 25px !important;
height: 25px !important;
position: absolute;
margin: 0px auto;
cursor: pointer;
}
.supermarket-icon:hover {
width: 30px !important;
height: 30px !important;
}
.bakery-icon {
background-image: url(./bakery.svg);
background-position: center;
background-repeat: no-repeat;
background-size: contain;
width: 25px !important;
height: 25px !important;
position: absolute;
margin: 0px auto;
cursor: pointer;
}
.bakery-icon:hover {
width: 30px !important;
height: 30px !important;
}
</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="jsStoreGeojsonCustomIcons.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) - SVG icon (
.svg
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:
- HTML
- JavaScript
- SVG store icon
- SVG supermarket icon
- SVG bakery icon
- Store locations in GeoJSON format
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.