📱Mixed platforms
If you are targeting Mobile (Android/iOS) and other platforms like Desktop, or the Browser. You will need to do some additional setup since the Mobile Geocoder/Geolocation services aren't available.
Geocoding example
This example is for a Compose Multiplatform app that targets mobile and Desktop, using Google Maps on non-mobile platforms. This is just an example, be sure to customize to your needs.
Declare Dependencies
First you will need to add the necessary dependencies:
Compass includes out of the box support for Google Maps and Mapbox. If you need support for another service you can use the geocoder-web
to implement your own, or Request a Geocoder API
Add dependencies & create source-set
Now you need to edit the build.gradle.kts
for your application module and add the following dependencies to the proper source-sets.
We will be creating a mobileMain
and nonMobileMain
source-set to minimize boilerplate. You can read about source-sets here.
Add platform expect/actual
This will produce the following folder structure, which you can then define a expect
function to create a Geocoder
instance:
Inside of commonMain/kotlin/src/Platform.kt
you need to define an expect
function that creates a Geocoder
instance:
Then you need to create the actual
functions:
And for the nonMobile
source set:
The Google Maps and Mapbox integration require API keys in order to use. If you want to customize the HttpClient
or the arguments passed to the API service, you can use the optional parameters. See Web API Service.
Using the Geocoder
Now that you are all setup you can use your createGeocoder()
function in the commonMain
source-set:
Last updated