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 MapKit on iOS and with MapLibre on Android. Users grant or decline location access on the screen before the map.
This recipe reuses the flow setup and the native views of the iOS recipe and the Android recipe. Each step of this recipe says what to copy from them, and what to do differently in a Unity project.
Before you start
- Update your app to Adapty Unity 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.
- Set up your app for custom elements, as in Display custom elements with the Unity SDK. This recipe adds code to the Java
CustomElementsclass from that guide. - Set up a permission handler in your C# code, as in Handle permission requests.
- On iOS, the map view needs iOS 17 or later. On iOS 16, users see the fallback.
1. Ask for location access
Add the location request to the flow as in step 1 of the iOS recipe.
When users tap Continue, the Adapty SDK sends the permission request to the C# handler of your app. To show the location dialog and pass the result to the flow, add the location_when_use case to the handler, as in Location while using the app. That section also lists the declarations for location access on iOS and Android.
2. Add the custom element to the screen
Add the custom element and its fallback image to the flow screen as in step 2 of the iOS recipe.
3. Add MapLibre to your Android project
To add MapLibre to your Android build, create the Assets/Plugins/Android/CustomElementsDependencies.androidlib folder. Unity adds this folder to the Gradle build as an Android library. Put the following files in it:
AndroidManifest.xml: One line,<manifest xmlns:android="http://schemas.android.com/apk/res/android" />.project.properties: One line,android.library=true.build.gradle:
apply plugin: 'com.android.library'
def unityLibraryAndroid = project(':unityLibrary').android
dependencies {
// api: the Java files in Assets/Plugins/Android can use these libraries
// the OpenGL artifact: Google Play also offers your app to devices without Vulkan
api 'org.maplibre.gl:android-sdk-opengl:13.8.0'
// ComponentActivity and the lifecycle classes that the map view uses
api 'androidx.activity:activity:1.8.2'
}
android {
namespace "com.example.app.dependencies"
compileSdk unityLibraryAndroid.compileSdk
buildToolsVersion = unityLibraryAndroid.buildToolsVersion
defaultConfig {
minSdk unityLibraryAndroid.defaultConfig.minSdk
targetSdk unityLibraryAndroid.defaultConfig.targetSdk
}
}4. Add the native code
iOS
In a Swift file in Assets/Plugins/iOS, add the place type, the location access model, the map view, and the resolver from steps 3 and 4 of the iOS recipe. The iOS recipe sets the resolver in Swift. Replace that code with a function that your C# code calls at launch:
import AdaptyUI
import CoreLocation
// Called once from C# at launch, before any flow is created.
@_cdecl("RegisterCustomElements")
public func RegisterCustomElements() {
Task { @MainActor in
// your app's places, such as partner fitness centers
let places = [
Place(name: "Central Fitness", coordinate: CLLocationCoordinate2D(latitude: 52.5219, longitude: 13.4132)),
Place(name: "Riverside Gym", coordinate: CLLocationCoordinate2D(latitude: 52.5163, longitude: 13.3777)),
]
AdaptyUI.setCustomElementsResolver(CustomElementsResolver(places: places))
}
}Android
Copy the Place, LocationAccess, and NearbyFitnessCentersMap classes from the Java tabs of step 3 of the Android recipe. Copy the CustomElementsResolver class from the Java tab of step 4 of the same recipe. Save each class in its own file in Assets/Plugins/Android, such as Place.java. Start each file with package com.example.app; and the imports that its class uses.
In CustomElements.java, list your places, and pass the resolver to the SDK:
package com.example.app;
import android.content.Context;
import com.adapty.ui.AdaptyUI;
import java.util.Arrays;
import java.util.List;
public final class CustomElements {
// your app's places, such as partner fitness centers
private static final List<Place> places = Arrays.asList(
new Place("Central Fitness", 52.5219, 13.4132),
new Place("Riverside Gym", 52.5163, 13.3777)
);
// Called once from C# at launch, before any flow is created.
public static void register(Context context) {
AdaptyUI.setCustomElementsResolver(new CustomElementsResolver(context, places));
}
}The SDK calls the resolver when the screen with the map opens. If the user granted location access, the resolver returns the map view. Otherwise, it returns no view, and users see the fallback. A permission change doesn’t make the SDK call the resolver again, so ask for location access on the screen before the map.
5. Test on a device
Test the flow as in step 5 of the iOS and Android recipes. Run a build of your app that includes the resolver and the permission handler.
To find out why the fallback appears instead of the map, follow the instructions in the Test on a device section of the Unity guide.