Skip to main content
GuidesAPI ReferenceExamples

Store Locator using Studio Defined Data Source

Estimated reading time: 6 minutes

This guide shows you how to build a store locator from scratch using store locations previously uploaded to the online Map Studio in GeoJSON format.

The finished example will look like this:

See the example fullscreen

When to use

  • The entire store location data set is relatively big.
  • The data set is changing frequently.

What is needed

  • Magic Lane API key token
  • Web server (an example is provided)
  • SVG file of the store logo (or use our sample SVG)
  • Store locations in GeoJSON format previously uploaded to the online Map Studio

Setup

Get your Magic Lane API key token: if you do not have a token, see the Getting Started guide.

info

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 stores data

 // Start by setting your token from https://developer.magiclane.com/api/projects
if (gem.core.App.token === undefined)
gem.core.App.token = "";

let defaultAppScreen = gem.core.App.initAppScreen(
{
container: "map-canvas",
center: [48.8562, 2.3516, 100000], // latitude , longitude, altitude
style: "./BasicStyle_with_Paris_shops_ex.style" // map style downloaded from Studio that contains your data source
});

let studioAddedControl = new gem.control.StudioAddedDataControl('954539838', '' /*default icon*/,
{
markerBubble: {
title: ['name'],
image: ['preview']
}
}
);
defaultAppScreen.addControl(studioAddedControl);

The map is rendered in the div container with the id map-canvas in the HTML page.
The map is centered on the specified position, given as latitude and longitude (in degrees) and altitude (in meters).
A map style is loaded.
//check - replace links & uncomment
gem.control.StudioAddedDataControl() is used to add GeoJSON data previously saved in the online Map Studio.
The id of the dataset from the Map Studio is provided, along with the filename of an SVG logo to use for rendering the store locations on the map.
The store locations are added to the map: defaultAppScreen.addControl(studioAddedControl);

Adding stores list

To display a list of the store locations add a menu list container to your HTML file and the following JavaScript code:

 let listUIControl = new gem.control.ListControl({
sourceControl: studioAddedControl,
flyToItemAltitude: 250,
container: 'menu-list-container',
menuName: 'Store locations',
titleProperties: ['name'],
detailsProperties: ['kinds'],
imageProperty: ['preview']
});
defaultAppScreen.addControl(listUIControl);

To add free-form text search functionality simply add the following JavaScript code:

 let searchControl = new gem.control.SearchControl({
searchPreferences: {
exactMatch: true,
maximumMatches: 3,
addressSearch: true,
mapPoisSearch: true,
setCursorReferencePoint: true
},
highlightOptions: {
contourColor: { r: 0, g: 255, b: 0, a: 0 }
}
});
defaultAppScreen.addControl(searchControl);

Complete example code

 // Start by setting your token from https://developer.magiclane.com/api/projects
if (gem.core.App.token === undefined)
gem.core.App.token = "";

let defaultAppScreen = gem.core.App.initAppScreen(
{
container: "map-canvas",
center: [48.8562, 2.3516, 100000], // latitude , longitude, altitude
style: "./BasicStyle_with_Paris_shops_ex.style" // map style downloaded from Studio that contains your data source
});

let studioAddedControl = new gem.control.StudioAddedDataControl('954539838', '' /*default icon*/,
{
markerBubble: {
title: ['name'],
image: ['preview']
}
}
);
let listUIControl = new gem.control.ListControl({
sourceControl: studioAddedControl,
flyToItemAltitude: 250,
container: 'menu-list-container',
menuName: 'Store locations',
titleProperties: ['name'],
detailsProperties: ['kinds'],
imageProperty: ['preview']
});
defaultAppScreen.addControl(studioAddedControl);
defaultAppScreen.addControl(listUIControl);

let searchControl = new gem.control.SearchControl({
searchPreferences: {
exactMatch: true,
maximumMatches: 3,
addressSearch: true,
mapPoisSearch: true,
setCursorReferencePoint: true
},
highlightOptions: {
contourColor: { r: 0, g: 255, b: 0, a: 0 }
}
});
defaultAppScreen.addControl(searchControl);

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)

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.

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.

See Store Locator using a GeoJSON File as Data Source for an example with GeoJSON data loaded from a file, and rendered on the map.

JavaScript Examples

Maps SDK for JavaScript Examples can be downloaded or cloned with Git.