Skip to content

Search Near a Location

Search for POIs (Points of Interest) around the current position or a specified location.

Overview

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.
See the example fullscreen

If you want to jump right in, you can download the HTML file and the JavaScript file for this example in the same directory, and then load the HTML file in a web browser to try it out right now!

Or you can continue reading for a detailed explanation of the code.

How it works

1// Start by setting your token from https://developer.magiclane.com/api/projects
2gem.core.App.token="your_API_key_token";
The first step in JavaScript is to set your API key token gem.core.App.token, which you can get at the Magic Lane website, see the Getting Started tutorial.
You only need to type your email address and create a new password.
 1// The main function
 2gem.core.App.registerInitialCallFunction(function () {
 3    // View of the map
 4    var defaultView = gem.core.App.getDefaultScreen().getDefaultMapView();
 5    var coordinates = { latitude: 48.8562, longitude: 2.3516, altitude: 10000, bearing: 0.0 };
 6    // Fly the camera to the center of the search area
 7    defaultView.centerOnCoordinates(coordinates, 0);
 8
 9    gem.core.App.registerConnectionStatusChanged(function (online) {
10        if (online) {
11            var result = new gem.core.LandmarkList();
12
13            // This function is called when the search completes
14            var callbackFunction = function (reason) {
15                var resultTouched = new gem.core.LandmarkList();
16                console.log("Reason is " + reason);
17                // Set the result highlight color (rgba) to red
18                defaultView.activateHighlight(result, { r: 255, g: 0, b: 0, a: 255 },
19                    gem.d3Scene.EHighlightOptions.EHO_ShowContour.value | gem.d3Scene.EHighlightOptions.EHO_ShowLandmark.value
20                    | gem.d3Scene.EHighlightOptions.EHO_Overlap.value | gem.d3Scene.EHighlightOptions.EHO_Group.value);
21                var cb = function (resultedTouches) { };
22                defaultView.registerLandmarkClickedEvent(resultTouched, cb);
23            };
24            // Create a filter and add point of interest (POI) categories to search for to the filter
25            var vCategories = gem.content.Manager.getGenericCategories();
26            var selectedFilter = new gem.core.LandmarkCategoryList();
27            selectedFilter.push_back(vCategories.get(0));
28            // Search around specified coordinates
29            gem.places.Search.searchPoiCategory(selectedFilter, coordinates, callbackFunction, result);
30        }
31    });
32});
33
34// Initializes the app
35gem.core.App.initApp();
The main function, where execution starts, is defined: gem.core.App.registerInitialCallFunction(function()
View of the map: var defaultView = gem.core.App.getDefaultScreen().getDefaultMapView();
Variable to hold POI (point of interest) search results: var result = new gem.core.LandmarkList();
Function called whenever app detects the device got connected or disconnected from the network: gem.core.App.registerConnectionStatusChanged(function(online)
Function called whenever a POI search completes: var callbackFunction = function(reason)
Set color for area perimeter, if any, red in this case: defaultView.activateHighlight(result,{r:255, g:0, b:0, a:255},
Get the generic POI categories to search for, a filter, and put the categories in a filter: var vCategories = gem.content.Manager.getGenericCategories(); var selectedFilter = new gem.core.LandmarkCategoryList(); selectedFilter.push_back(vCategories.get(0));
Fly the camera to the specified coordinates, 0 milliseconds flight time means jump there instantly: defaultView.centerOnCoordinates(coordinates,0);
Search around specified coordinates - as these are now the current coordinates, this is equivalent to searching around current position (reverse geocoding): gem.places.Search.searchPoiCategory(selectedFilter,coordinates,callbackFunction,result);
 1<!DOCTYPE html>
 2<html lang="en-us">
 3  <head>
 4    <meta charset="utf-8" />
 5    <title>Search Nearby - MagicLane Maps SDK for JavaScript</title>
 6    <link rel="stylesheet" type="text/css" href="https://www.magiclane.com/sdk/js/gem.css" />
 7    <style>
 8     body {
 9       overflow: hidden;
10     }
11 </style>
12  </head>
13  <body>
14    <div id="wrapper">
15      <div class="profile-main-loader">
16        <div class="loader">
17          <svg class="circular-loader" viewBox="25 25 50 50">
18            <circle class="loader-path" cx="50" cy="50" r="20" fill="none" stroke="#70c542" stroke-width="2" />
19          </svg>
20        </div>
21      </div>
22    </div>
23    <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex="0"></canvas>
24    <script src="https://www.magiclane.com/sdk/js/gemapi.js"></script>
25    <script type="text/javascript" src="token.js"></script>
26    <script type="text/javascript" src="jsSearchNearby.js"></script>
27  </body>
28</html>
The canvas is the drawing area where the map is rendered. The canvas is configured to fill the browser window.
Note that the div elements must be defined before loading the JavaScript source.
The circular-loader is the rotating circle animation seen while loading.
At the bottom, gemapi.js, the Maps SDK for JavaScript is loaded.
Next, the JavaScript source of this example is loaded.
The example is 2 plain text files, one with the HTML code (.html file extension) and the other with the JavaScript code (.js file extension).
To run the example, the HTML file is loaded in a browser. The .js file should be in the same directory, as it will be loaded automatically.

Source code for this example:

HTML and JavaScript

right-click on the links and select Save As.

JavaScript Examples

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