跳转到内容

适用于 Android Development Kit (ADK) 的 Google Maps 地图功能调试工具

在 ADK 中得到支持。PythonTypeScriptGoJavaKotlin预览

Google Maps 的该工具通过 Gemini 模型,将您的 Agent Development Kit (ADK) 中的代理程序与 Google Maps 的地理空间数据连接起来。当用户提出与位置相关的问题时,该模型会内部调用 Google Maps 来获取关于地点、评论、营业时间以及路线等信息,而无需执行任何本地代码。

预览

Google Maps 的“Grounding Tool”目前仍处于预览阶段(即尚未正式发布)。该工具可能存在一些功能限制,因此不建议在生产环境中使用。

服务使用要求

当您使用 Google Maps 的数据来展示结果时,必须在您的应用程序中明确显示相关的 Google Maps 数据来源及出处信息。有关完整的展示要求、出处标注及使用规范,请参阅《使用 Google Maps 的相关文档》(Grounding with Google Maps Documentation)。 Gemini API 或者 代理平台(Agent Platform).

使用场景

  • 本地发现与推荐功能:根据用户的偏好和地理位置,推荐附近的餐厅、咖啡馆或其他服务。
  • 与特定地点相关的问题:回答有关营业时间、菜单、餐厅特色、无障碍设施、电动汽车充电站以及客户评价等方面的问题。
  • 旅行与行程规划制定每天的行程计划,估算旅行时间,并在沿途找到有趣的景点。

先决条件

  • 这是一个受支持的 Gemini 模型。请参阅相关文档中的受支持模型列表。 Gemini API 或者 代理平台(Agent Platform) documentation.
  • 已为……配置了凭据(Credentials)。 Google Cloud Agent Platform通过设置(by setting) GOOGLE_GENAI_USE_ENTERPRISE=TRUE, GOOGLE_CLOUD_PROJECT,以及 GOOGLE_CLOUD_LOCATION.
  • 已为您的目标语言安装了 ADK。请参阅…… ADK 快速入门指南 有关安装说明,请参阅……(请提供具体的安装说明文件或链接)。

与代理程序(agent)一起使用。

将内置的 Google Maps 功能添加到您的代理工具列表中。该模型会根据用户的输入来决定何时向 Google Maps 发送请求(即何时查询地图信息)。

from google.adk.agents import Agent
from google.adk.tools import google_maps_grounding

root_agent = Agent(
    name="maps_grounding_agent",
    model="gemini-flash-latest",
    description="Answers location and place questions using Google Maps.",
    instruction=(
        "You are a local guide. Use Google Maps to answer questions about "
        "places, business hours, reviews, and nearby recommendations."
    ),
    tools=[google_maps_grounding],
)
import { GOOGLE_MAPS_GROUNDING, LlmAgent } from "@google/adk";

const rootAgent = new LlmAgent({
  name: "maps_grounding_agent",
  model: "gemini-flash-latest",
  description: "Answers location and place questions using Google Maps.",
  instruction:
    "You are a local guide. Use Google Maps to answer questions about " +
    "places, business hours, reviews, and nearby recommendations.",
  tools: [GOOGLE_MAPS_GROUNDING],
});

export { rootAgent };
package main

import (
    "context"
    "log"

    "google.golang.org/adk/v2/agent"
    "google.golang.org/adk/v2/agent/llmagent"
    "google.golang.org/adk/v2/model/gemini"
    "google.golang.org/adk/v2/tool"
    "google.golang.org/adk/v2/tool/geminitool"
    "google.golang.org/genai"
)

func newMapsAgent(ctx context.Context) (agent.Agent, error) {
    model, err := gemini.NewModel(ctx, "gemini-flash-latest", &genai.ClientConfig{})
    if err != nil {
        return nil, err
    }

    return llmagent.New(llmagent.Config{
        Name:        "maps_grounding_agent",
        Model:       model,
        Description: "Answers location and place questions using Google Maps.",
        Instruction: "You are a local guide. Use Google Maps to answer questions about places, business hours, reviews, and nearby recommendations.",
        Tools:       []tool.Tool{geminitool.GoogleMaps{}},
    })
}

func main() {
    if _, err := newMapsAgent(context.Background()); err != nil {
        log.Fatal(err)
    }
}
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.GoogleMapsTool;

LlmAgent rootAgent =
    LlmAgent.builder()
        .name("maps_grounding_agent")
        .model("gemini-flash-latest")
        .description("Answers location and place questions using Google Maps.")
        .instruction(
            "You are a local guide. Use Google Maps to answer questions about "
                + "places, business hours, reviews, and nearby recommendations.")
        .tools(GoogleMapsTool.INSTANCE)
        .build();
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.Gemini
import com.google.adk.kt.tools.GoogleMapsTool

val rootAgent =
    LlmAgent(
        name = "maps_grounding_agent",
        model = Gemini(name = "gemini-flash-latest"),
        description = "Answers location and place questions using Google Maps.",
        instruction =
            Instruction(
                "You are a local guide. Use Google Maps to answer questions about " +
                    "places, business hours, reviews, and nearby recommendations.",
            ),
        tools = listOf(GoogleMapsTool()),
    )

提供位置相关信息(即“位置上下文”)。

当用户提出类似“我附近的咖啡店”这样的查询时,需要将用户的地理位置坐标传递给代理程序的内容生成模块,以便模型能够根据该区域提供相应的搜索结果。ADK Python、ADK TypeScript、ADK Go 和 ADK Java 都支持在搜索配置中设置纬度和经度坐标;而在 ADK Kotlin 中,则可以直接在提示信息或代理程序的指令中指定具体的城市、社区或地址。

from google.adk.agents import Agent
from google.adk.tools import google_maps_grounding
from google.genai import types

root_agent = Agent(
    name="maps_grounding_agent",
    model="gemini-flash-latest",
    description="Answers location-aware questions using Google Maps.",
    instruction=(
        "Recommend nearby places based on the user's location and preferences."
    ),
    tools=[google_maps_grounding],
    generate_content_config=types.GenerateContentConfig(
        tool_config=types.ToolConfig(
            retrieval_config=types.RetrievalConfig(
                lat_lng=types.LatLng(
                    latitude=37.7749,
                    longitude=-122.4194,
                ),
                language_code="en_US",
            )
        )
    ),
)
import { GOOGLE_MAPS_GROUNDING, LlmAgent } from "@google/adk";

const rootAgent = new LlmAgent({
  name: "maps_grounding_agent",
  model: "gemini-flash-latest",
  description: "Answers location-aware questions using Google Maps.",
  instruction:
    "Recommend nearby places based on the user's location and preferences.",
  tools: [GOOGLE_MAPS_GROUNDING],
  generateContentConfig: {
    toolConfig: {
      retrievalConfig: {
        latLng: {
          latitude: 37.7749,
          longitude: -122.4194,
        },
        languageCode: "en_US",
      },
    },
  },
});

export { rootAgent };
package main

import (
    "context"
    "log"

    "google.golang.org/adk/v2/agent"
    "google.golang.org/adk/v2/agent/llmagent"
    "google.golang.org/adk/v2/model/gemini"
    "google.golang.org/adk/v2/tool"
    "google.golang.org/adk/v2/tool/geminitool"
    "google.golang.org/genai"
)

func newLocalizedMapsAgent(ctx context.Context) (agent.Agent, error) {
    model, err := gemini.NewModel(ctx, "gemini-flash-latest", &genai.ClientConfig{})
    if err != nil {
        return nil, err
    }

    return llmagent.New(llmagent.Config{
        Name:        "maps_grounding_agent",
        Model:       model,
        Description: "Answers location-aware questions using Google Maps.",
        Instruction: "Recommend nearby places based on the user's location and preferences.",
        Tools:       []tool.Tool{geminitool.GoogleMaps{}},
        GenerateContentConfig: &genai.GenerateContentConfig{
            ToolConfig: &genai.ToolConfig{
                RetrievalConfig: &genai.RetrievalConfig{
                    LatLng: &genai.LatLng{
                        Latitude:  genai.Ptr(37.7749),
                        Longitude: genai.Ptr(-122.4194),
                    },
                    LanguageCode: "en_US",
                },
            },
        },
    })
}

func main() {
    if _, err := newLocalizedMapsAgent(context.Background()); err != nil {
        log.Fatal(err)
    }
}
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.GoogleMapsTool;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.LatLng;
import com.google.genai.types.RetrievalConfig;
import com.google.genai.types.ToolConfig;

LlmAgent rootAgent =
    LlmAgent.builder()
        .name("maps_grounding_agent")
        .model("gemini-flash-latest")
        .description("Answers location-aware questions using Google Maps.")
        .instruction(
            "Recommend nearby places based on the user's location and preferences.")
        .tools(GoogleMapsTool.INSTANCE)
        .generateContentConfig(
            GenerateContentConfig.builder()
                .toolConfig(
                    ToolConfig.builder()
                        .retrievalConfig(
                            RetrievalConfig.builder()
                                .latLng(
                                    LatLng.builder()
                                        .latitude(37.7749)
                                        .longitude(-122.4194)
                                        .build())
                                .languageCode("en_US")
                                .build())
                        .build())
                .build())
        .build();

检查接地相关的元数据。

当模型使用 Google Maps 来生成响应内容时,响应事件会包含一些元数据(即用于描述响应内容的额外信息),其中包含一系列用于描述具体地理位置的数据片段(称为 “grounding chunks”)。每个这样的数据片段都会提供地点的名称、对应的 Google Maps URI 以及地点的 ID。这样,您的应用程序就可以正确地显示所需的引用信息及相关链接了。

{
  "groundingChunks": [
    {
      "maps": {
        "uri": "https://maps.google.com/?cid=...",
        "title": "Example Cafe",
        "placeId": "places/..."
      }
    }
  ]
}

使用 title 和 uri 从每个 Maps 数据块中提取需要显示的值 根据显示和署名要求,提供 Google Maps 的源链接 Gemini API 或者 代理平台(Agent Platform).

可用的工具

工具名称 SDK 符号 描述
google_maps Python: google_maps_grounding (GoogleMapsGroundingTool)
TypeScript: GOOGLE_MAPS_GROUNDING (GoogleMapsGroundingTool)
Go: geminitool.GoogleMaps
Java: GoogleMapsTool.INSTANCE (GoogleMapsTool)
Kotlin: GoogleMapsTool
在 Gemini 模型的请求中,支持使用 Google Maps 实现服务器端的“接地”(Grounding)功能。

限制条件(或:局限性)

  • 仅适用于 Gemini 型号的产品。该工具仅支持受支持的 Gemini 型号;不支持非 Gemini 型号或 Gemini 1.x 版本的型号。
  • 没有本地函数的执行。由于 Google Maps 的相关功能是在 Gemini 后端系统中执行的,因此该工具并未提供客户端函数声明(即客户端代码中可以直接调用的函数),因此你无法将其直接作为本地函数来使用。
  • 与其他工具结合使用根据 Gemini 模型的不同版本,您可能无法将内置的“接地”功能(grounding tools)与其他工具在同一代理(agent)中同时使用。对于这类问题,可以采取一些变通方案,例如将代理封装在一个额外的容器或框架中(即“wrap the agent in an additional container or framework”)。 AgentTool请参阅: ADK工具的局限性.
  • 内置的接地机制与模型上下文协议(Model Context Protocol, MCP):请使用这个内置工具,将 Gemini 模型与 Google 地图上的地点、评论及导航信息进行关联。如果您需要在客户端进行工具调用,或者希望将非 Gemini 模型连接到 Google Maps Grounding Lite,請参阅相关文档。 MCP 工具.

其他资源