Back to APIs

Google Maps

maps.google.com

Use location data and maps with Google Maps.

Using the Google Maps API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Google Maps background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Example code using Google Maps

Below is a working code example of how you can use Google Maps with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import { Client } from "@googlemaps/google-maps-services-js";
3
import z from "zod";
4
5
// Get your API key here: https://console.developers.google.com/apis/credentials
6
// Make sure to enable the Google Maps Data API https://console.cloud.google.com/apis/library/maps-backend.googleapis.com
7
// SDK Docs: https://developers.google.com/maps/documentation/javascript/overview
8
// Initialize the Google Maps API with your API key
9
const apiKey = String(process.env.GOOGLE_MAP_API_KEY);
10
11
const map = new Client({});
12
13
client.defineJob({
14
id: "google-map-geocode",
15
name: "Google Map Geocode",
16
version: "1.0.0",
17
trigger: eventTrigger({
18
name: "google-map-geocode",
19
schema: z.object({
20
address: z.string(),
21
}),
22
}),
23
run: async (payload, io, ctx) => {
24
const { address } = payload;
25
26
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
27
await io.runTask(
28
"Google Map Geocode",
29
async () => {
30
// Make the geocode request
31
const response = await map.geocode({
32
params: {
33
address,
34
key: apiKey,
35
},
36
});
37
38
// Process the geocode response here
39
return JSON.parse(JSON.stringify(response.data));
40
},
41
42
// Add metadata to improve how the task displays in the logs
43
{ name: "Google map geocode", icon: "google" }
44
);
45
},
46
});