Free Text Search
This guide shows you how to use free text search control on an interactive map with various options.
When to use
- To render a searchable interactive map.
What is needed
- Magic Lane API key token
- Web server (an example is provided)
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.
Free Text Search Control using Default Options
The finished example using default search control options will look like this:
If the search result is a region, its contour/perimeter is rendered on the map in red.
The map supports pan and zoom, and is fully 3D, so holding down the shift key and panning will simultaneously rotate and tilt the map.
How it works
- JavaScript
- HTML
// 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.8612, 2.333, 7000000] // latitude , longitude, altitude
}
);
let searchControl = new gem.control.SearchControl();
defaultAppScreen.addControl(searchControl);
The map is rendered in the div
container with the id map-canvas
in the HTML page.
The map is initially centered on the specified coordinates, given as latitude and longitude in degrees, and altitude in meters.
A defaultAppScreen.addControl(searchControl);
is added to the map, which includes a free text search box.
f the search result is an area, such as a country or region, its perimeter will be drawn on the map. The color of the perimeter can be optionally specified using rgba (red, green, blue, alpha) values between 0 and 255, otherwise a default color will be used.
<html>
<head>
<meta charset="utf-8">
<title>Search Example - MagicLane Maps SDK for JavaScript</title>
<link rel="stylesheet" type="text/css" href="https://www.magiclane.com/sdk/js/gem.css">
</head>
<body>
<div id="map-canvas" style="width: 100%; height: 100%; position: absolute;"></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="jsHelloSearch01.js"></script>
</body>
</html>
map-canvas
is the id of the drawing area where the map is rendered. The canvas is configured to fill the browser window.
At the bottom, gemapi.js, the Maps SDK for JavaScript is loaded.
Next, the JavaScript source of this example is loaded.
Free Text Search Control Custom Options
The finished example using customized search control options will look like this:
- JavaScript
- HTML
// 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.8612, 2.333, 7000000] // latitude , longitude, altitude
}
);
let searchControl = new gem.control.SearchControl(
{
// search options
highlightOptions: {
contourColor: { r: 0, g: 255, b: 0, a: 255 },
searchResultPin: false
},
searchPreferences: {
exactMatch: true,
maximumMatches: 4,
addressSearch: true,
mapPoisSearch: true,
mapEnvelope: { // restrict to Paris area
topLeft: {
latitude: 49.02391,
longitude: 1.98219,
altitude: 0,
bearing: 0
},
bottomRight: {
latitude: 48.61471,
longitude: 2.79206,
altitude: 0,
bearing: 0
}
},
setCursorReferencePoint: true
},
searchResults: {
initialPlaceSearch: "Paris",
showLandmarkIcons: true
}
}
);
defaultAppScreen.addControl(searchControl);
The search control options used in this example are:
highlightOptions.contourColor
: search result highlight contour color (if available)highlightOptions.searchResultPin
set tofalse
: highlight result landmark icon instead of using search result pin iconsearchPreferences.exactMatch
set totrue
: find exact match related to the search input text stringsearchPreferences.maximumMatches
: number of search results to display, maximum 5searchPreferences.addressSearch
set totrue
: use the address searchsearchPreferences.mapPoisSearch
set totrue
: use the POIs searchsearchPreferences.mapEnvelope
: map area in which to restrict the search resultssearchPreferences.setCursorReferencePoint
set totrue
: the search reference coordinate is set at the last cursor position, if set tofalse
(default) the reference point is the center of the map envelopesearchResults.initialPlaceSearch
: specify a start-up locationsearchResults.showLandmarkIcons
set totrue
: display the landmark icons next to the search results
<html>
<head>
<meta charset="utf-8">
<title>Search Example - MagicLane Maps SDK for JavaScript</title>
<link rel="stylesheet" type="text/css" href="https://www.magiclane.com/sdk/js/gem.css">
</head>
<body>
<div id="map-canvas" style="width: 100%; height: 100%; position: absolute;"></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="jsHelloSearch02.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)
To run the example, the HTML file is loaded in a browser.
Source code for default options search control:
Source code for customized options search control:
Right-click on the links and select Save As.
JavaScript Examples
Maps SDK for JavaScript Examples can be downloaded or cloned with Git.