10.0: Geocode locations in natural language with interaction automations and Geoapify #9
jstanden
started this conversation in
Guides and Tutorials
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Introduction
Let's assume we need to convert user-provided natural language locations (e.g. street addresses, landmarks) into geolocation coordinates (latitude/longitude) for visualizing on a map.
This process is called "geocoding".
In this example, we'll build a geocoding service in Cerb 10 that you can reuse in any workflow.
Sign up for the Geoapify API
Geocoding requires a large dataset of map locations. It doesn't make sense for us to ship this with Cerb. Instead, we're going to integrate with an API from Geoapify.
If you don't already have a Geoapify account, you can start with a free one here: https://www.geoapify.com/pricing
This same approach will work with any geocoding API: Google Maps, OpenWeatherMap, Geocodeapi, etc.
Import the example package in Cerb
Navigate to Setup >> Packages >> Import and paste the following package:
Click the Import button.
Paste your Geoapify API key from above.
Click the Import button again.
Geocoding
Reload the page and click on the floating, circular Cerb icon in the lower right.
The menu has a new Geocode Location interaction.
Click that and an interaction will start. You can enter a street address, city, country, or landmark to find its coordinates.
cerb10_interaction_geocode.mov
This example simply displays the location on a map. You could modify it to meet your needs: insert coordinates into an email draft, set a custom field, find store locations in proximity to the coordinates, and so on.
How it works
Connected service and account
The package imports and configures a connected service for Geoapify. It then creates a connected account for that service with the API key. That account is used by automations to interact with the API.
You'll find these in Search -> Connected Services and Search -> Connected Accounts.
Automations
The package imports two automations. You'll find these in Search -> Automations.
wgm.services.geoapify.geocode
This automation function "wraps" the geocoding API from Geoaplify. It takes a human readable location as input and sends that to the API through an HTTP request. It also uses the connected account above to securely append the API key to the request.
Any automation can run this function to turn a location into coordinates. The possible locations are returned as the
geojson
key.The policy allows the automation to use the http.request command for any URL beginning with
https://api.geoapify.com/v1/geocode/search?
.wgm.example.geoapify.location
This interaction automation prompts a worker for a location (using continuations), uses the function above to generate a list of possible locations, prompts the user to choose one of those, then it displays the selected coordinates and plots them on a map.
The map prompt uses Maps KATA to draw the map. The automation generates that KATA on-the-fly.
Interactions can also return data to their "caller". In this example the global menu doesn't expect any results. For something like the email editor the interaction could return a
snippet:
to be inserted at the cursor.The policy allows this automation to use the function: command with the
wgm.services.geoapify.geocode
automation above.Toolbar
The package modifies the global.menu toolbar to add the interaction. You'll find this in Search -> Toolbars.
You can add this interaction to other locations (e.g. organization cards) by adding it to other toolbars.
Beta Was this translation helpful? Give feedback.
All reactions