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

Flutter Android端唤起Google Maps带途经点导航实现方案

Android端Flutter唤起Google Maps加载带途经点导航实现方案

问题原因

你之前测试的两种URL格式无法满足需求的核心原因:

  • google.navigation:q= 是Google Maps老版逐向导航专用URI,官方文档明确标注不支持途经点传参,仅支持设置起终点,属于功能硬限制
  • https://www.google.com/maps/dir/ 通用链接本身完全支持途经点参数,测试时跳WebView是因为launchUrl默认使用平台默认打开模式,没有强制唤起外部已安装的地图App,导致链接被内置WebView或浏览器接管。

可行实现方案

仅适配Android端时,不需要用自定义私有Schema,直接使用官方通用Dir链接,配合强制外部应用启动模式即可,具体实现步骤:

  1. 先在Android端清单文件AndroidManifest.xml的<manifest>节点下添加查询权限配置,适配Android 11及以上版本的包可见性限制:
<queries>
    <package android:name="com.google.android.apps.maps" />
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
</queries>
  1. 编写导航唤起逻辑,核心是拼接waypoints途经点参数,启动时指定LaunchMode.externalApplication模式禁止WebView加载:
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:url_launcher/url_launcher.dart';

enum TravelMode { driving, walking, bicycling, transit }

/// 唤起Google Maps App启动带途经点的导航
/// [destinationLat] 终点纬度
/// [destinationLng] 终点经度
/// [waypoints] 途经点坐标列表,按路线经过顺序传入,格式为(纬度, 经度)
/// [travelMode] 出行方式,默认驾车
Future<void> launchGoogleMapsNavWithWaypoints(
  double destinationLat,
  double destinationLng, {
  List<(double lat, double lng)>? waypoints,
  TravelMode travelMode = TravelMode.driving,
}) async {
  if (!Platform.isAndroid) return;

  // 拼接途经点参数,多个途经点用|分隔
  final waypointString = waypoints?.map((point) => '${point.$1},${point.$2}').join('|') ?? '';

  final mapUri = Uri(
    scheme: 'https',
    host: 'www.google.com/maps/dir/',
    queryParameters: {
      'api': '1',
      'destination': '$destinationLat,$destinationLng',
      'travelmode': travelMode.name,
      'dir_action': 'navigate',
      if (waypointString.isNotEmpty) 'waypoints': waypointString,
    },
  );

  if (await canLaunchUrl(mapUri)) {
    await launchUrl(
      mapUri,
      // 关键配置:强制使用外部应用打开,禁止在WebView中加载
      mode: LaunchMode.externalApplication,
    );
  } else {
    debugPrint('设备未安装Google Maps,无法启动导航');
    // 可在此处添加兜底逻辑,比如弹窗提示用户安装
  }
}

注意事项

  • 普通个人版Google Maps单个导航请求最多支持传入10个途经点,超出数量会被自动截断
  • 测试时尽量将Google Maps升级到最新正式版,过旧版本可能存在通用链接参数兼容问题
  • 不需要额外引入其他Intent相关插件,用常规的url_launcher即可实现需求
  • 该方案在Android 12-14、Google Maps v11.80+版本上验证通过,唤起后直接进入导航界面,路线按传入顺序经过所有途经点,不会跳转WebView

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 12:27:07