This tutorial covers a common UX pattern for Android mapping apps, adding map markers from external data using GeoJSON and presenting additional information about the marker on tap with a slide-in UI panel using ModalBottomSheet
.
Your final map will include markers over 3 coffee shops in Providence, which you can tap to find out more information about the business:
To follow along with this guide you'll need:
Android
mode of the editor. Make sure Android Studio shows Android
in the view in your top left corner of your editor, as opposed to Project
, Production
, etc.XML
, which contains a vector-based marker image, like an SVG. Download this XML
file by clicking the first download button.If you wish to copy the full code snippet in its entirety instead of following along with the tutorial, view the full code snippet here.
If you only copy the snippet, there will be two missing dependencies, make sure to download and add the XML
and the GeoJSON
files from above and add them to your project.
Before starting this tutorial, it's recommend following the Get Started with the Maps SDK for Android.
This getting started guide will show you how to configure your credentials, add the Mapbox dependency to your project and add a map to your application.
Once you are able to successfully render a map, you can move onto the next step of this tutorial, Add a marker to your map.
For this step, add a marker to your map by creating a PointAnnotation
.
A PointAnnotation
is a UI element that is fixed to a location on the map. For this step you will use a vector graphic of a map marker to create a PointAnnotation
. To learn more about other types of annotations, see our Annotation Guide.
You can add the marker by following the steps below:
MainActivity.kt
file (excluding your package import line on line 1).ic_blue_marker.xml
by clicking the blue download button if you haven't already.XML
file to the app > res > drawables folder by dragging it in from your file explorer/finder window.Your should see the map zoomed over a marker in Providence, Rhode Island, USA as shown in the screenshot at the bottom of the page.
Now that you've learned how to add a marker to your map, this step will show you how to use a GeoJSON file to add multiple markers to your map. GeoJSON
is a data format that can store information about point-of-interests (POI) such as their coordinates, business name, address, phone number, etc. You can use the coordinates written in the GeoJSON
file to determine where our markers will be placed.
To add a GeoJSON
file to your project and create markers based on its data, follow these steps:
Step 1: Add the GeoJSON
file to your project:
GeoJSON
file if you haven't already.GeoJSON
file into the assets folder.Now that the GeoJSON
file has been added to your project, you can now add the markers.
Step 2: Read the data from the GeoJSON
file and pass this information through to the markers to set their locations:
import org.json.JSONObject
dependency from the first section of the code snippet.PointAnnotation
code and replace it with the second highlighted code snippet below. This code reads the geojson file, parses it into a FeatureCollection
type, then iterates over each Feature
with a forEach
loop. For each feature, it adds a PointAnnotation
to the map.The app should now show 3 markers hovering over three coffee shops in Providence, Rhode Island, as seen in the image at the bottom of the page.
Throughout the rest of this tutorial, each of these code snippets with show hidden lines
, when fully expanded can be copied and added to your project if you do not wish to individually update sections of the code.
If you follow this method, make sure your update your package import
line at the top of your code each time.
If you have difficulty dragging in the GeoJSON file into the Android Studio editor, you can also add it through the finder window, by going to PROJECTFOLDER/app/src/main/assets
.
Now that you've spawned markers based on the GeoJSON
data, next you'll add tap functionality and grab the properties data about the marker to display the business name of the coffee shop, by using the onClick
method:
import android.widget.Toast
dependency as seen in the first code snippet section.forEach
loop, grab the name
property of each coffee shop from its GeoJSON
Feature, as seen in the second highlighted section in the code snippet below.PointAnnotation
function, to add interactionsState.onClicked
. This function detects when a user taps on an annotation and allows you to execute some code. In this case, the code will create a Toast to show the tap has successfully occurred and that the associated properties data is available for display.
interactionsState.onClicked{
code, the left bracket {
is on the same line as interactionsState.onClicked
, otherwise it may throw an error.Now when you click on the markers, the name of the respective coffee shop will appear in the toast at the bottom of the simulator. You can also see this in action in the video below.
The tap events executing and marker properties data available, the next step is to set up a ModalBottomSheet
to appear when a marker is tapped. This UI element is a nice-looking dialog you can use to present data from the tapped feature. For now you can add the ModalBottomSheet
with placeholder text so you can focus on getting the open and close logic the way you want it.
ModalBottomSheet
dependencies as seen in the first snippet section below.ModalBottomSheet
, above override fun onCreate
as seen in the second snippet section.
ModalBottomSheet
is an experimental class from Android Studio that may be changed in the future.sheetState
and showBottomSheet
at the top of setContent
to manage ModalBottomSheet
and determine if the sheet should be opened or closed, as seen in the third snippet section.Toast
code with showBottomSheet = true
inside of the interactionsState.onClicked
function, as seen in the fourth snippet section.
ModalBottomSheet
with content when it opens, creating a Text
element that reads Marker Tapped!.When you run the simulator, you should be able to click on a marker and have the ModalBottomSheet
appear with text that says "Marker Clicked!". You can see this in action in the video at the bottom of the page.
Now that the ModalBottomSheet
appears on click, next you'll add text objects that grab from the marker data and populate the sheet. You'll need to create global variables the ModalBottomSheet
can check when it renders and assign those values the relevant data when the marker is clicked.
To create and update these values, follow these steps:
public class MainActivity : ComponentActivity()
as seen in the second snippet section.
ModalBottomSheet
can update them properly.GeoJSON
as seen in the third code snippet.interactionsState.onClicked
, update the global variables with the values you grabbed from the GeoJSON
as seen in the fourth code snippet.Text
element in ModalBottomSheet
with the code from the last section of the snippet.
Now, you can click on each of the markers and see their data appear in the ModalBottomSheet
as seen in the video at the bottom of the page.
You've now made a map with markers using GeoJSON data, and added tap interactivity to display properties to the user using the Mapbox Maps SDK for Android. This interaction is a foundation user experience of map-based apps that present point-based features, and you're ready to move on to more advanced functionality.
If you would like to view the finished code in its entirety, view the Finished Code Snippet at the bottom of the page.
Lastly, if you experienced any issues during the integration, here is a list of common troubleshooting solutions:
Now that you've finished this tutorial, here are some other relevant resources:
Here are some ideas for improving this project if you would like to continue working on it:
ModalBottomSheet
to make it more visually appealing.