You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Dialogflow与NodeJS Client构建地图/路线卡片及集成操作意图

解决方案:在Dialogflow中用Node.js Client构建路线/地图卡片

Great question! You're right that Dialogflow does offer native components to streamline building map/routing cards, and while there isn't a dedicated "route card" type out of the box, we can combine its built-in features with Google Maps services to achieve exactly what you need. Here's a breakdown of how to approach this:

一、用Dialogflow原生卡片快速实现基础路线展示

Dialogflow的Node.js Client提供了BasicCard组件,我们可以把通过Google Directions API生成的静态地图嵌入其中,同时添加DIRECTIONS等操作按钮,完美适配内置意图的触发逻辑。

示例代码:

const { dialogflow, BasicCard, Button } = require('actions-on-google');
const app = dialogflow();

// 处理自定义的路线查询意图
app.intent('Request Route', (conv) => {
  // 假设已通过Google Directions API获取到静态地图URL和导航链接
  const staticMapUrl = '你的静态地图生成URL';
  const navigationUrl = 'Google Maps导航跳转链接';

  conv.ask(new BasicCard({
    title: '前往目的地的最优路线',
    image: {
      url: staticMapUrl,
      alt: '路线预览地图'
    },
    buttons: new Button({
      title: '立即开始导航',
      url: navigationUrl
    })
  }));
});

二、集成Dialogflow内置的DIRECTIONS系统意图

Dialogflow内置的actions.intent.DIRECTIONS意图可以自动识别用户的导航请求(比如"帮我去XX地"),你只需在Agent后台启用该意图,就能在代码中直接获取起点、终点等参数:

// 处理内置的DIRECTIONS意图
app.intent('actions.intent.DIRECTIONS', (conv, params) => {
  const origin = params.location.origin.name;
  const destination = params.location.destination.name;
  
  // 调用Google Directions API获取路线数据和静态地图(此处省略API调用逻辑)
  const staticMapUrl = '生成的静态地图URL';
  const navigationUrl = `https://www.google.com/maps/dir/?api=1&origin=${origin}&destination=${destination}`;
  
  conv.ask(`这是从${origin}到${destination}的路线预览:`);
  conv.ask(new BasicCard({
    image: { url: staticMapUrl, alt: '路线地图' },
    buttons: new Button({ title: '打开导航', url: navigationUrl })
  }));
});

三、进阶:用自定义Payload实现高度定制化卡片

如果需要更灵活的样式(比如直接展示Polyline路线),可以通过自定义Payload适配不同平台(如Google Assistant、Facebook Messenger)。以Google Assistant为例:

app.intent('Custom Route Card', (conv) => {
  const customPayload = {
    "richResponse": {
      "items": [
        {
          "simpleResponse": {
            "textToSpeech": "这是为你生成的专属路线地图"
          }
        },
        {
          "basicCard": {
            "title": "实时导航路线",
            "image": {
              "url": "静态地图URL",
              "accessibilityText": "路线预览"
            },
            "buttons": [
              {
                "title": "启动导航",
                "openUrlAction": {
                  "url": "Google Maps导航链接"
                }
              },
              {
                "title": "分享路线",
                "openUrlAction": {
                  "url": "路线分享链接"
                }
              }
            ]
          }
        }
      ]
    }
  };
  
  conv.ask(customPayload);
});

四、补充:Polyline转静态地图的简化逻辑

你提到的用Polyline生成静态地图的方法非常实用,这里提供一个快速生成URL的代码片段:

const polyline = '从Directions API获取的Polyline字符串';
const apiKey = '你的Google Maps API密钥';
const staticMapUrl = `https://maps.googleapis.com/maps/api/staticmap?size=600x300&path=enc:${polyline}&key=${apiKey}`;

注意:确保你的Google Cloud项目已启用Maps Static API和Directions API,并配置了有效的API密钥。

内容的提问来源于stack exchange,提问作者AllJs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:59:01