Displaying a Map
Your environment is now fully configured, and you have the necessary Google Maps and MapsIndoors API keys. Next you will learn how to load a map with MapsIndoors and display it in a React Native app.
Selecting and Loading a Solution
To start off, we set up a simple two screen navigation using the @react-navigation
package. We will create a FrontScreen component that lets us chose the solution to open, and a MapScreen component that will display the actual map.
App.tsx
The FrontScreen
is defined as a functional component function FrontScreen(/*...*/) {/*...*/}
that returns the View
to be displayed. The property navigation
gives us access to the navigator and lets us navigate to other screens.
We use a useState
hook apiKeyString
for storing the API key entered by the user and to pass it to the next screen when navigating.
As for what to actually render, we return a react fragment containing a button with a preset API key for a demo solution. And following that, a row containing an input box and a button for using the entered API key. In order to navigate to the MapScreen, we call navigation.navigate('Map', {apiKey: apiKeyString})}
where 'Map'
is the name we gave the screen in the navigator, and we pass the API key in the parameters, which will be available through route
on the following screen.
The FrontScreen
and app App
are already implemented for you.
screens/FrontScreen.tsx
Showing the map
For the MapScreen
we create a functional component similarly to the above. Note the additional property route
which contains parameters from the navigation stack, such as the API key we provided from the FrontScreen
From the basic example you are now left to implement the load
function as well as the useEffect
hook and adding the MapView
to the returned view.
We store the reference to our MapControl
in a useState
hook, so that we can pass it to other components in the future. We define a load
function, which gets the API key from route.params
. We use it to load our solution data through MapsIndoors
, create a MapControl
that is used to control the displayed map, and navigate to the first venue in the solution by using MapControl
. Be sure to await
the asynchronous API calls to know when it has fully loaded.
We use a useEffect
hook to load when entering the MapScreen
.
Actually showing the Map is pretty simple, consisting of returning a MapView
component, styled to set the appropriate size.
Currently there is a limitation that on some platforms the size of the MapView
must be provided in actual dimensions, as opposed to percentages or flex
. We can get the size of the screen using useWindowDimensions()
, and use flex: 1
to reduce the size to fit.
Here is the first implementation of the MapScreen
, with the logic described above.
Last updated