Grounding with Google Maps  |  Firebase AI Logic

Grounding with Google Maps

Grounding with Google Maps connects a Gemini model to geospatial data from Google Maps so that you can build location-aware functionality into your apps.

Supported models

General-use Gemini 2.5 models support this capability, but they're all deprecated.

Ground the model with Google Maps

Click your Gemini API provider to view provider-specific content
and code on this page.
Gemini Developer APIAgent Platform Gemini API

When you create the GenerativeModel instance, provide GoogleMaps as a tool that the model can use to generate its response.

import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Example: Coordinates for New York City
let latAndLong = CLLocationCoordinate2D(latitude: 40.7128, longitude: -74.0060)

// (Optional) Define a RetrievalConfig to configure the Grounding with Google Maps tool.
let retrievalConfig = RetrievalConfig(
    location: latAndLong,
    languageCode: "en-US"
)

// Wrap the RetrievalConfig inside a ToolConfig.
let toolConfig = ToolConfig(retrievalConfig: retrievalConfig)

// Create a `GenerativeModel` instance with a model that supports your use case.
let model = ai.generativeModel(
    modelName: "GEMINI_MODEL_NAME",
    tools: [Tool.googleMaps()],
    toolConfig: toolConfig
)

let response = try await model.generateContent("restaurants near me?")
print(response.text ?? "No text in response.")

Grounding with Google Maps has the following benefits:

Best practices and tips to improve results

This section describes some general best practices for using Grounding with Google Maps, as well as how to take advantage of place properties to improve results.

General best practices

Use place properties in prompts

This section lists place properties that are used to describe locations and are used by Grounding with Google Maps to generate responses.

Sample place properties

Example prompts that use place properties

How Grounding with Google Maps works

When you provide the model with the GoogleMaps tool, the model handles the entire workflow of searching, processing, and citing information automatically.

Here's the workflow of the model:

  1. Receives prompt: Your app sends a prompt to the Gemini model with the GoogleMaps tool enabled.
  2. Analyzes prompt: The model analyzes the prompt and determines if Google Maps can improve its response.
  3. Invokes the tool: The model, recognizing the geographical intent, invokes the Grounding with Google Maps tool.
  4. Sends queries to Google Maps: The Grounding with Google Maps service queries Google Maps for relevant information.
  5. Processes the Google Maps results: The model processes the Google Maps results and formulates a response to the original prompt.
  6. Returns a Google Maps Grounded Result: The model returns a final, user-friendly response that is grounded in the Google Maps results.

Understand the grounded result

If the model grounds its response in Google Maps results, then the response includes a groundingMetadata object that contains structured data.

Example response that includes a groundingMetadata object:

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "CanteenM is an American restaurant with..."
          }
        ],
        "role": "model"
      },
      "groundingMetadata": {
        "groundingChunks": [
          {
            "maps": {
              "uri": "https://maps.google.com/?cid=13100894621228039586",
              "title": "Heaven on 7th Marketplace",
              "placeId": "places/ChIJ0-zA1vBZwokRon0fGj-6z7U"
            }
          }
        ],
        "groundingSupports": [
          {
            "segment": {
              "startIndex": 0,
              "endIndex": 79,
              "text": "CanteenM is an American restaurant with a 4.6-star rating and is open 24 hours."
            },
            "groundingChunkIndices": [0]
          }
        ]
      }
    }
  ]
}

Service usage requirements

This section describes the service usage requirements for Grounding with Google Maps for your chosen Gemini API provider.

Inform users of Google Maps sources

With each Google Maps Grounded Result, you'll receive sources in groundingChunks that support each response. The following metadata is also returned:

Pricing and rate limits

Grounding with Google Maps pricing is based on queries. A request is only counted towards the Google Maps quota when a prompt successfully returns at least one Google Maps Grounded Result.