Getting Started: MapsIndoors
Throughout the Getting Started guide, you will modify and extend the same code to create your map. Let's start by creating the initial app.
Step 1. Set Up Your Environment
If you do not have prior development experience, you can install an Integrated Development Environment (IDE), e.g. Visual Studio Code.
Start by creating a new project folder. The location is not important, just remember the location, and ensure your newly created project folder is empty.
Inside that, create two empty files:
index.htmlandmain.js.The file
index.htmlis the entry point for our application and contains the HTML code. The filemain.jswill be read byindex.htmland consists of the JavaScript code for the actual application to run. To try the app you will be creating, runindex.htmlin your web browser.Open
index.html. Create a basic HTML structure and include themain.jsfile as follows:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MapsIndoors</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>Both here, and in the following examples, you will always be able to see which of the two files the code should go in, by looking at the first line, where the name of the file is written.
Your environment is now fully configured, and you have the necessary API keys. Next, you will learn how to display a map with MapsIndoors.
Step 2. Display a Map with MapsIndoors
The MapsIndoors SDK is hosted on a Content Delivery Network (CDN) and should be loaded using a script tag.
Insert the MapsIndoors SDK script tag into <head>, followed by the Google Maps script tag:
Remember to add your API keys to the links in your code. If you do not have your own API key, you can use the demo MapsIndoors API key: d876ff0e60bb430b8fabb145.
Add an empty <div> element to <body> with the id attribute set to "map":
To load data and display it on the map, we need to create a new instance of the MapsIndoors class with a mapView instance with a few properties set. This is all done by placing the following code in the main.js file you created earlier:
What happens in this snippet is we create a mapViewInstance that pulls up a GoogleMapsView with some mapViewOptions. The options define which element in the html-file to display the map in (in this case <div id="map">), where the map should center, what zoom level to display, and what the max zoom level is.
You should now see a map from your chosen map engine with MapsIndoors data loaded on top.
Display a Floor Selector
Next, we'll add a Floor Selector for changing between floors.
First, we add an empty <div> element programmatically. Then we create a new FloorSelector instance and push the floorSelectorElement to the googleMapsInstance to position it as a map controller:
You should now be able to switch between floors.
Here's a JSFiddle demonstrating the result you should have by now:
Using the <mi-map-googlemaps> component, the MapsIndoors JS SDK is automatically inserted into the DOM when initialized.
The MapsIndoors Web Components library can be loaded using unpkg, a widely used CDN for everything on npm.
Insert script tag into <head>:
Check @mapsindoors/components for latest version.
After you added the script tag into <head>, add the <mi-map-googlemaps> custom element into <body>. We need to add and populate the gm-api-key and mi-api-key attributes with your API keys as well:
Remember to add your API keys where indicated. You can use the demo MapsIndoors API key: d876ff0e60bb430b8fabb145
To center the map correctly, you need need the Google Maps instance in your JavaScript-file.
First, we get a reference to the <mi-map-googlemaps> element. Then we attach the mapsIndoorsReady event listener so we'll know when MapsIndoors is ready after loading. Lastly, on the mapsIndoorsReady event, get the Google Maps instance and call its setCenter method to center the map on the loaded data:
For more information on how to configure the
<mi-map-googlemaps>component, see components.mapsindoors.com/map-googlemaps.
You should now see a map from your chosen map engine with MapsIndoors data loaded on top.
Display a Floor Selector
Next, we'll add a Floor Selector for changing between floors.
Using the <mi-map-googlemaps> element, you can add the floorSelectorControlPosition attribute to your existing element. In this case with the value "TOP_RIGHT":
You should now be able to switch between floors.
Here's a JSFiddle demonstrating the result you should have by now:
The MapsIndoors SDK is hosted on a Content Delivery Network (CDN) and should be loaded using a script tag.
Insert the MapsIndoors SDK script tag into <head>, followed by the Mapbox script and style tag:
Remember to add your API keys to the links in your code. You can use the demo MapsIndoors API key:
d876ff0e60bb430b8fabb145.
Add an empty <div> element to <body> with the id attribute set to "map":
To load data and display it on the map, we need to create a new instance of the MapsIndoors class with a mapView instance with a few properties set. This is all done by placing the following code in the main.js file you created earlier:
What happens in this snippet is we create a mapViewInstance that pulls up a MapboxView with some mapViewOptions. The options define which element in the html-file to display the map in (in this case <div id="map">), where the map should center, what zoom level to display, and what the max zoom level is.
You should now see a map from your chosen map engine with MapsIndoors data loaded on top.
Display a Floor Selector
Next, we'll add a Floor Selector for changing between floors.
First, we add an empty <div> element programmatically. Then we create a new FloorSelector instance and push the floorSelectorElement to the mapboxInstance to position it as a map controller:
See all available control positions in the Mapbox Documentation.
You should now be able to switch between floors.
Here's a JSFiddle demonstrating the result you should have by now:
The MapsIndoors SDK is hosted on a Content Delivery Network (CDN) and should be loaded using a script tag.
Insert the MapsIndoors SDK script tag into <head>, followed by the Mapbox script and style tag:
Remember to add your API keys to the links in your code. You can use the demo MapsIndoors API key:
d876ff0e60bb430b8fabb145.
Add an empty <div> element to <body> with the id attribute set to "map":
To load data and display it on the map, we need to create a new instance of the MapsIndoors class with a mapView instance with a few properties set. This is all done by placing the following code in the main.js file you created earlier:
What happens in this snippet is we create a mapViewInstance that pulls up a MapboxView with some mapViewOptions. The options define which element in the html-file to display the map in (in this case <div id="map">), where the map should center, what zoom level to display, and what the max zoom level is. You can also control mapsIndoorsTransitionLevel which defines when Mapbox' Extruded buildings disappear and MapsIndoors data is shown. lightPreset sets the global light for you application, while showMapMarkers property defines if Mapbox' Markers are shown on the map.
You should now see a map from your chosen map engine with MapsIndoors data loaded on top.
Display a Floor Selector
Next, we'll add a Floor Selector for changing between floors.
First, we add an empty <div> element programmatically. Then we create a new FloorSelector instance and push the floorSelectorElement to the mapboxInstance to position it as a map controller:
See all available control positions in the Mapbox Documentation.
You should now be able to switch between floors.
Using the <mi-map-mapbox> component, the MapsIndoors JS SDK is automatically inserted into the DOM when initialized.
The MapsIndoors Web Components library can be loaded using unpkg, a widely used CDN for everything on npm.
Insert script tag into <head>:
Check @mapsindoors/components for latest version.
After you added the script tag into <head>, add the <mi-map-mapbox> custom element into <body>. We need to add and populate the accessToken and mi-api-key attributes with your access token and API key as well:
Remember to add your API keys where indicated. You can use the demo MapsIndoors API key: d876ff0e60bb430b8fabb145.
To center the map correctly, you need need the Mapbox instance in your JavaScript-file.
First we get a reference to the <mi-map-mapbox> element. Then we attach the mapsIndoorsReady event listener so we'll know when MapsIndoors is ready after loading. Lastly, on the mapsIndoorsReady event, get the Mapbox instance and call its setCenter method to center the map on the loaded data:
For more information on how to configure the <mi-map-mapbox> component, see components.mapsindoors.com/map-mapbox.
You should now see a map from your chosen map engine with MapsIndoors data loaded on top.
Display a Floor Selector
Next, we'll add a Floor Selector for changing between floors.
Using the <mi-map-mapbox> element, you can add the floorSelectorControlPosition attribute to your existing element. In this case with the value "TOP_RIGHT":
See all available control positions in the Mapbox Documentation.
You should now be able to switch between floors.
Here's a JSFiddle demonstrating the result you should have by now:
Step 3. Create a Search Experience
In this step, you'll create a simple search and display the search results in a list. You'll also learn how to filter the data displayed on the map.
Create a Simple Query Search
MapsIndoors Locations can be retrieved in the MapsIndoors namespace using the LocationsService.getLocations() method but first you need to add a <input> and <button> element to the DOM.
Create an
<input>and<button>element in<body>.Attach an
onclickevent to the<button>element and call aonSearchmethod, which you will create next.
Create the
onSearchmethod.Get a reference to the search
<input>element.Define a new object with the search parameter
qand the value ofsearchInputElement.Call the
getLocationsmethod and log out the results to the console.
See all available search parameters in the reference documentation.
Display a List of Search Results
To display a list of search results you can append each search result to a list element.
Add the
<ul>list element below the search field in<body>with theidattribute set to "search-results".
Get a reference to the list element.
Reset the list on every complete search.
Add a for loop and append every result to the search results list element.
Filter Locations on Map Based on Search Results
To filter the map to only display the search results you can use the filter method.
Call
mapsIndoorsInstance.filterwith an array of Location IDs.
To remove the location filter again, call mapsIndoorsInstance.filter(null).
Here's a JSFiddle demonstrating the result you should have by now:
Create a Simple Query Search
Using the <mi-search> component you get a <input>element tied tightly together with the Location Service.
Insert the
<mi-search>custom element into<body>.Add the
mapsindoorsandplaceholderattributes.
Get a reference to the
<mi-search>element.Attach an
resultsevent listener and log out the results to the console.
For more information on available events and how to configure the <mi-search> component, see components.mapsindoors.com/search.
Display a List of Search Results
To display a list of search results you can append each search result to a list element.
Insert the
<mi-list>custom element below the search field in<body>.Add the
scroll-buttons-enabledandscroll-lengthattributes.
For more information on how to configure the <mi-list> component, see components.mapsindoors.com/list.
Get a reference to the list element.
Reset the list on every complete search.
Add a for loop and append every result to the search results list element.
Filter Locations on Map Based on Search Results
To filter the map to only display the search results you can use the filter method.
To remove the location filter again, call mapsIndoorsInstance.filter(null).
Here's a JSFiddle demonstrating the result you should have by now:
Create a Simple Query Search
MapsIndoors Locations can be retrieved in the MapsIndoors namespace using the LocationsService.getLocations() method but first you need to add an <input> and <button> element to the DOM.
Create an
<input>and<button>element in<body>.Attach an
onclickevent to the<button>element and call aonSearchmethod, which you will create next.
Create the
onSearchmethod.Get a reference to the search
<input>element.Define a new object with the search parameter
qand the value ofsearchInputElement.Call the
getLocationsmethod and log out the results to the console.
See all available search parameters in the reference documentation.
Display a List of Search Results
To display a list of search results you can append each search result to a list element.
Add the
<ul>list element below the search field in<body>with theidattribute set to "search-results".
Get a reference to the list element.
Reset the list on every complete search.
Add a for loop and append every result to the search results list element.
Filter Locations on Map Based on Search Results
To filter the map to only display the search results you can use the filter method.
Call
mapsIndoorsInstance.filterwith an array of Location IDs.
To remove the location filter again, call mapsIndoorsInstance.filter(null).
Here's a JSFiddle demonstrating the result you should have by now:
https://jsfiddle.net/mapspeople/r86903om/3/
Create a Simple Query Search
Using the <mi-search> component you get an <input>element tied tightly together with the Location Service.
Insert the
<mi-search>custom element into<body>.Add the
mapsindoorsandplaceholderattributes.
Get a reference to the
<mi-search>element.Attach a
resultsevent listener and log the results in the console.
For more information on available events and how to configure the <mi-search> component, see components.mapsindoors.com/search.
Display a List of Search Results
To display a list of search results you can append each search result to a list element.
Insert the
<mi-list>custom element below the search field in<body>.Add the
scroll-buttons-enabledandscroll-lengthattributes.
For more information on how to configure the <mi-list> component, see components.mapsindoors.com/list.
Get a reference to the list element.
Reset the list on every complete search.
Add a for loop and append every result to the search results list element.
Filter Locations on Map Based on Search Results
To filter the map to only display the search results you can use the filter method.
To remove the location filter again, call mapsIndoorsInstance.filter(null).
Here's a JSFiddle demonstrating the result you should have by now:
Step 4. Getting Directions
In this step you'll create directions between two points and change the transportation mode.
Step 4a. Get Directions Between Two Locations
To get directions between two MapsIndoors Locations, or places outside of your MapsIndoors solution, we need two things:
The Directions Service instance
The Directions Render instance
We need the Directions Service to calculate the fastest route between two points, and use the Directions Render to actually draw the route on the map.
Get Directions Service and Render instances
First, initialize the MapsIndoors Directions Service, and add an external Directions Provider (in this case Google Maps).
Then, we need to initialize the MapsIndoors Directions Render with the MapsIndoors instance:
See all available directions render options and methods in the reference documentation.
Now our app is ready to provide directions. Next up is how to give it an Origin and Destination - and draw a route between those.
Draw a Route on the Map
To display a route on the map, we use the coordinates of an Origin and Destination and draw a line between them. For this, we use MapsIndoors' DirectionsRenderer.
The Destination coordinate is retrieved dynamically using the coordinate of the selected Location in the search results list. Therefore, you must search for the destination to get directions, and then click the result in the text list. Different solutions can of course be implemented into your own solution later.
In this tutorial, the Origin is, naturally, a hardcoded coordinate in the demo API key supplied with this guide. If you're using you own key, you can hardcode coordinates from a Location in your building instead.
In the following example, this is what happens:
Create a new
getRoutemethod inmain.jswhich accepts a locationCreate two new constants, one for the Origin's coordinate, and another for the Destination's coordinate
Add another constant defining the
routeParametersUsing the MapsIndoors Directions Service call the
getRoutemethod to get the fastest route between the two coordinatesSee all available route parameters in the reference documentation.
Using the MapsIndoors Directions Renderer call the
setRoutemethod to display the route on the map
Now, to make it more dynamic, we attach a click event listener for each location appended to the search results list element with the getRoute method as a callback function.
You will now have something like this:
Now you can click on any item in the search results list to get directions from the hardcoded origin to that destination.
Change Transportation Mode
In MapsIndoors, the transportation mode is referred to as travel mode. There are four travel modes; walking, bicycling, driving and transit (public transportation). The travel modes apply for outdoor navigation. Indoor navigation calculations are based on walking travel mode.
To change between travel modes we first need to add a <select> element with all four transportation options above the search field:
To use the chosen travel mode when getting a route, we need to replace the hardcoded value for travelMode parameter inside the getRoute method with the <select> elements value:
You now have something like this:
Get Directions Service and Render instances
First, add two new let statements all the way at the top, after the miMapElement constant for storing our Directions Service and Directions Renderer instances. Then, we populate them with the instances within the mapsIndoorsReady event:
See all available directions render options and methods in the reference documentation.
Now our app is ready to provide directions. Next up is how to give it an Origin and Destination - and draw a route between those.
Draw a Route on the Map
Now our app is ready to provide directions. Next up is how to give it an Origin and Destination - and draw a route between those.
Draw a Route on the Map
To display a route on the map, we use the coordinates of an Origin and Destination and draw a line between them. For this, we use MapsIndoors' DirectionsRenderer.
The Destination coordinate is retrieved dynamically using the coordinate of the selected Location in the search results list. Therefore, you must search for the destination to get directions, and then click the result in the text list. Different solutions can of course be implemented into your own solution later.
In this tutorial, the Origin is, naturally, a hardcoded coordinate in the demo API key supplied with this guide. If you're using you own key, you can hardcode coordinates from a Location in your building instead.
In the following example, this is what happens:
Create a new
getRoutemethod inmain.jswhich accepts alocationCreate two new constants, one for the Origin's coordinate, and another for the Destination's coordinate
Add another constant defining the
routeParametersUsing the MapsIndoors Directions Service call the
getRoutemethod to get the fastest route between the two coordinatesSee all available route parameters in the reference documentation.
Using the MapsIndoors Directions Renderer call the
setRoutemethod to display the route on the map
Now, to make it more dynamic, we attach a click event listener for each location appended to the search results list element with the getRoute method as a callback function.
Now you can click on any item in the search results list to get directions from the hardcoded origin to that destination.
Change Transportation Mode
In MapsIndoors, the transportation mode is referred to as travel mode. There are four travel modes; walking, bicycling, driving and transit (public transportation). The travel modes apply for outdoor navigation. Indoor navigation calculations are based on walking travel mode.
To change between travel modes we first need to add a <select> element with all four transportation options above the search field:
To use the chosen travel mode when getting a route, we need to replace the hardcoded value for travelMode parameter inside the getRoute method with the <select> elements value:
You now have something like this:
Get Directions Service and Render instances
First, initialize the MapsIndoors Directions Service, and add an external Directions Provider (in this case Mapbox).
Then, we need to initialize the MapsIndoors Directions Renderer with the MapsIndoors instance:
See all available directions render options and methods in the reference documentation.
Now our example app is ready to provide Directions. Next up is how to give it an Origin and Destination and draw the route between.
Draw a Route on the Map
To display a route on the map, we use the coordinates of an Origin and Destination and draw a line between them. For this, we use MapsIndoors' Directions Renderer.
The Destination coordinate is retrieved dynamically, using the coordinate of the selected Location in the search results list. Therefore, you must search for the destination to get directions, and then click the result in the text list. Different solutions can of course be implemented into your own solution later.
In this tutorial, the Origin is, naturally, a hardcoded coordinate in the demo API key supplied with this guide. If you're using you own key, you can hardcode coordinates from a Location in your building instead.
In the following example, this is what happens:
Create a new
getRoutemethod inmain.jswhich accepts alocationCreate two new constants, one for the Origin's coordinate, and another for the Destination's coordinate
Add another constant defining the
routeParametersUsing the MapsIndoors Directions Service call the
getRoutemethod to get the fastest route between the two coordinatesSee all available route parameters in the reference documentation.
Using the MapsIndoors Directions Renderer call the
setRoutemethod to display the route on the map
Now, to make it more dynamic, we attach a click event listener for each location appended to the search results list element with the getRoute method as a callback function.
Now you can click on each item in the search results list to get directions.
Change Transportation Mode
In MapsIndoors, the transportation mode is referred to as travel mode. There are four travel modes; walking, bicycling, driving and transit (public transportation). The travel modes apply for outdoor navigation. Indoor navigation calculations are based on walking travel mode.
To change between travel modes we first need to add a <select> element with all four transportation options above the search field:
To use the chosen transportation when getting a route, we need to replace the hardcoded value for travelMode parameter inside the getRoute method with the <select> elements value:
You now have something like this:
Get Directions Service and Render instances
First, add two new let statements all the way at the top, after the miMapElement constant for storing our Directions Service and Directions Renderer instances. Then, we populate them with the instances within the mapsIndoorsReady event:
See all available directions render options and methods in the reference documentation.
Now our app is ready to provide directions. Next up is how to give it an Origin and Destination - and draw a route between those.
Draw a Route on the Map
To display a route on the map, we use the coordinates of an Origin and Destination and draw a line between them. For this, we use MapsIndoors' Directions Renderer.
The Destination coordinate is retrieved dynamically, using the coordinate of the selected Location in the search results list. Therefore, you must search for the destination to get directions, and then click the result in the text list. Different solutions can of course be implemented into your own solution later.
In this tutorial, the Origin is, naturally, a hardcoded coordinate in the demo API key supplied with this guide. If you're using you own key, you can hardcode coordinates from a Location in your building instead.
In the following example, this is what happens:
Create a new
getRoutemethod inmain.jswhich accepts alocationCreate two new constants, one for the Origin's coordinate, and another for the Destination's coordinate
Add another constant defining the
routeParametersUsing the MapsIndoors Directions Service call the
getRoutemethod to get the fastest route between the two coordinatesSee all available route parameters in the reference documentation.
Using the MapsIndoors Directions Renderer call the
setRoutemethod to display the route on the map
Now, to make it more dynamic, we attach a click event listener for each location appended to the search results list element with the getRoute method as a callback function.
Now you can click on any item in the search results list to get directions from the hardcoded origin to that destination.
Change Transportation Mode
In MapsIndoors, the transportation mode is referred to as travel mode. There are four travel modes; walking, bicycling, driving and transit (public transportation). The travel modes apply for outdoor navigation. Indoor navigation calculations are based on walking travel mode.
To change between travel modes we first need to add a <select> element with all four transportation options above the search field:
To use the chosen travel mode when getting a route, we need to replace the hardcoded value for travelMode parameter inside the getRoute method with the <select> elements value:
You now have something like this:
Last updated
Was this helpful?