利用 Google 地圖建立基準

利用 Google 地圖建立基準,讓 Gemini 的生成式功能連結至 Google 地圖豐富、真實且最新的資料。開發人員可以輕鬆將位置辨識功能整合至自家應用程式。如果使用者查詢的內容與地圖資料相關,Gemini 模型會利用 Google 地圖提供準確且即時的答案,並與使用者指定的確切位置或大概區域相關。

  • 準確的地理位置感知回覆:針對特定地理位置的查詢,運用 Google 地圖的豐富最新資料。
  • 強化個人化功能:根據使用者提供的地點,量身打造推薦內容和資訊。

開始使用

本範例說明如何將 Grounding with Google Maps 整合至應用程式,根據使用者查詢提供準確的回覆,並瞭解相關位置資訊。提示會要求提供當地建議,並可選擇提供使用者位置資訊,讓 Gemini 模型使用 Google 地圖資料。

Python

# This will only work for SDK newer than 2.0.0
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.6-flash",
    input="What are the best Italian restaurants within a 15-minute walk from here?",
    tools=[{
        "type": "google_maps",
        "latitude": 34.050481,
        "longitude": -118.248526
    }]
)

# Print the model's text response and annotations
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)
                if content_block.annotations:
                    print("\nSources:")
                    for annotation in content_block.annotations:
                        if annotation.type == "place_citation":
                            print(f"  - {annotation.name}: {annotation.url}")

JavaScript

// This will only work for SDK newer than 2.0.0
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const interaction = await ai.interactions.create({
    model: "gemini-3.6-flash",
    input: "What are the best Italian restaurants within a 15-minute walk from here?",
    tools: [{
      type: "google_maps",
      latitude: 34.050481,
      longitude: -118.248526
    }]
  });

  // Print the model's text response and annotations
  for (const step of interaction.steps) {
    if (step.type === 'model_output') {
      for (const contentBlock of step.content) {
        if (contentBlock.type === 'text') {
          console.log(contentBlock.text);
          if (contentBlock.annotations) {
            console.log("\nSources:");
            for (const annotation of contentBlock.annotations) {
              if (annotation.type === 'place_citation') {
                console.log(`  - {annotation.name}: {annotation.url}`);
              }
            }
          }
        }
      }
    }
  }
}

main();

REST

# Specifies the API revision to avoid breaking changes when they become default
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.6-flash",
    "input": "What are the best Italian restaurants within a 15-minute walk from here?",
    "tools": [{
      "type": "google_maps",
      "latitude": 34.050481,
      "longitude": -118.248526
    }]
  }'

如何利用 Google 地圖建立基準

利用 Google 地圖建立基準的服務會使用 Maps API 做為基準來源,將 Gemini API 與 Google 地理位置生態系統整合。如果使用者查詢包含地理位置背景資訊,Gemini 模型可以叫用「以 Google 地圖建立基準」工具。模型接著會根據與所提供位置相關的 Google 地圖資料,生成回覆。

整個程序通常涵蓋下列工作:

  1. 使用者查詢:使用者向應用程式提交查詢,可能包含地理位置背景資訊 (例如「我附近的咖啡店」、「舊金山的博物館」)。
  2. 工具呼叫:Gemini 模型會辨識地理意圖, 並呼叫「利用 Google 地圖建立基準」工具。這項工具可選擇性提供使用者的 latitudelongitude。這項工具是文字搜尋工具,運作方式與在 Google 地圖上搜尋類似,也就是說,系統會使用座標來處理本地查詢 (「我附近」),而特定或非本地查詢則不太會受到明確位置的影響。
  3. 資料擷取:「利用 Google 地圖建立基準」服務會查詢 Google 地圖,找出相關資訊 (例如地點、評論、相片、地址、營業時間)。
  4. 以擷取資料為基準生成內容:系統會使用擷取的 Google 地圖資料,做為 Gemini 模型回覆的依據,確保內容符合事實且具關聯性。
  5. 回應和註解:模型會傳回文字回應,並附上連結至 Google 地圖來源的內嵌註解,方便開發人員顯示引用內容。

使用 Google 地圖建立基準的原因與時機

如果應用程式需要準確、最新且特定地點的資訊,就非常適合使用「利用 Google 地圖建立基準」功能。這項功能會根據 Google 地圖全球超過 2.5 億個地點的龐大資料庫,提供相關且個人化的內容,提升使用者體驗。

如果應用程式需要執行下列操作,請使用「利用 Google 地圖建立基準」功能:

  • 完整且如實回答特定地區的問題。
  • 打造對話式行程規劃工具和當地導覽。
  • 根據位置和使用者偏好 (例如餐廳或商店) 推薦搜尋點。
  • 為社交、零售或外送服務打造位置感知體驗。

在需要鄰近位置和當前事實資料的應用情境中,例如尋找「我附近最好的咖啡店」或取得路線指引,以 Google 地圖為基礎的搜尋結果會是最佳選擇。

用途

利用 Google 地圖建立基準支援各種需要位置資訊的用途。

處理地點相關問題

詳細詢問特定地點的問題,根據 Google 使用者評論和其他地圖資料取得解答。

Python

# This will only work for SDK newer than 2.0.0
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.6-flash",
    input="Is there a cafe near the corner of 1st and Main that has outdoor seating?",
    tools=[{
        "type": "google_maps",
        "latitude": 34.050481,
        "longitude": -118.248526
    }]
)

for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)
                if content_block.annotations:
                    print("\nSources:")
                    for annotation in content_block.annotations:
                        if annotation.type == "place_citation":
                            print(f"  - {annotation.name}: {annotation.url}")

JavaScript

// This will only work for SDK newer than 2.0.0
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const interaction = await ai.interactions.create({
    model: "gemini-3.6-flash",
    input: "Is there a cafe near the corner of 1st and Main that has outdoor seating?",
    tools: [{
      type: "google_maps",
      latitude: 34.050481,
      longitude: -118.248526
    }]
  });

  for (const step of interaction.steps) {
    if (step.type === 'model_output') {
      for (const contentBlock of step.content) {
        if (contentBlock.type === 'text') {
          console.log(contentBlock.text);
          if (contentBlock.annotations) {
            console.log("\nSources:");
            for (const annotation of contentBlock.annotations) {
              if (annotation.type === 'place_citation') {
                console.log(`  - ${annotation.name}: ${annotation.url}`);
              }
            }
          }
        }
      }
    }
  }
}

main();

提供以位置為依據的個人化服務

根據使用者的偏好和特定地理區域,取得量身打造的建議。

Python

# This will only work for SDK newer than 2.0.0
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.6-flash",
    input="Which family-friendly restaurants near here have the best playground reviews?",
    tools=[{
        "type": "google_maps",
        "latitude": 30.2672,
        "longitude": -97.7431
    }]
)

for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)
                if content_block.annotations:
                    print("\nSources:")
                    for annotation in content_block.annotations:
                        if annotation.type == "place_citation":
                            print(f"  - {annotation.name}: {annotation.url}")

JavaScript

// This will only work for SDK newer than 2.0.0
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const interaction = await ai.interactions.create({
    model: "gemini-3.6-flash",
    input: "Which family-friendly restaurants near here have the best playground reviews?",
    tools: [{
      type: "google_maps",
      latitude: 30.2672,
      longitude: -97.7431
    }]
  });

  for (const step of interaction.steps) {
    if (step.type === 'model_output') {
      for (const contentBlock of step.content) {
        if (contentBlock.type === 'text') {
          console.log(contentBlock.text);
          if (contentBlock.annotations) {
            console.log("\nSources:");
            for (const annotation of contentBlock.annotations) {
              if (annotation.type === 'place_citation') {
                console.log(`  - ${annotation.name}: ${annotation.url}`);
              }
            }
          }
        }
      }
    }
  }
}

main();

協助規劃行程

生成多日行程,提供各個地點的路線和資訊,非常適合用於旅遊應用程式。

Python

# This will only work for SDK newer than 2.0.0
from google import genai

client = genai.Client()

prompt = "Plan a day in San Francisco for me. I want to see the Golden Gate Bridge, visit a museum, and have a nice dinner."

interaction = client.interactions.create(
    model="gemini-3.6-flash",
    input