Show nearby places on a map in a flow

Follow this recipe to include an interactive map in a flow. The map shows your places, such as partner fitness centers, within 2 km of the user. Your app renders the map as a native view in a custom element, with MapLibre. Your app requests location access on the flow screen.

Before you start

  • Update your app to Adapty Android SDK 4.4 or later. Earlier SDK versions show the fallback content for every custom element.
  • Set up your app to fetch and display flows.
  • To ask users for location access, display flows in an activity that extends ComponentActivity, such as AppCompatActivity.
  • Add MapLibre to your app with Gradle, from org.maplibre.gl:android-sdk-opengl. With this artifact, Google Play also offers your app to devices without Vulkan, which the default android-sdk artifact requires.
  • Set minSdk to 23 or higher, as MapLibre requires.

To limit your app to approximate location, add the following inside the <manifest> element of AndroidManifest.xml. Make sure that the <manifest> element declares xmlns:tools="http://schemas.android.com/tools".

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- removes precise location, which MapLibre declares -->
<uses-permission
    android:name="android.permission.ACCESS_FINE_LOCATION"
    tools:node="remove" />

1. Add the custom element to the screen

In the Flow & Paywall Builder, add a custom element to a flow screen, and set it up for the map:

  • Type: map.
  • ID: fitness_centers.
  • Height: Keep the default Fixed height. The map fills the custom element.

Keep the custom element out of containers that respond to touch, such as a carousel, so that only the map responds to a drag on it.

Inside the custom element, add an Image with a picture of a map. Users see it as the fallback when:

  • Their version of your app doesn’t display the map.
  • They denied location access before the screen opened.

If the user hasn’t answered the location access prompt yet, your app shows it when the map appears. To tell users why your app needs their location, add a text element above the custom element, such as “Find fitness centers near you.”

2. Create the map view

Create a type for your places, and a model for location access:

Create a view with a map. The view requests location access and displays a marker for each of your places within 2 km of the user:

If the user denies location access on the screen, the view replaces the map with a message about how to allow access in Settings.

3. Match the map to the custom element

In your app-wide resolver, add properties for the LocationAccess model and your places. Then return the map view for the map Type and the fitness_centers ID. The highlighted lines show what to add or change. If your app has no app-wide resolver yet, copy the whole code:

The SDK calls the resolver when the screen opens, and again each time it redraws the custom element while the screen is open. Location access determines what the resolver returns on each call:

  • Not answered yet: The map view, so that the view can request access.
  • Allowed: The map view, which shows your places near the user.
  • Denied before the screen opened: No view, so users see the fallback instead of the message about Settings.
  • Denied on the screen: The map view, because the view sets isMapOnScreen while it’s on the screen. If the resolver returned no view here, the SDK would replace the message about Settings with the fallback at the next call.

Create the resolver with your places, and set it as the app-wide resolver. Call AdaptyUI.setCustomElementsResolver once, such as at app launch:

4. Test on a device

The Flow & Paywall Builder can’t run your app code, so its canvas only shows the fallback. To preview the map:

  1. Publish the flow. To see the latest published version instead of a cached one, register the device as a test device.
  2. To see a marker on the map, add a place within 2 km of your location to your list of places.
  3. Run a build of your app that includes the resolver. Open the screen, and allow location access. Verify that the map shows your location and a marker for the place that you added.

To test the fallback, in the Settings app, turn off location access for your app. Open the screen, and if the prompt appears, deny location access. The map view shows the message about Settings. Open the screen again, and verify that the fallback appears.

To find out why the fallback appears instead of the map, follow the instructions in the Test on a device section of the Android guide.