Flutter如何跳转外部地图app导航

使用url_launcher跳转到外部地图导航

Flutter版本
1.22.0.stable

在pubspec.yaml中添加插件

url_launcher: 5.7.10

新建一个 url_open_utils.dart文件,作为工具类。

import 'dart:io';

import 'package:url_launcher/url_launcher.dart';
import 'package:water_app/widget/base/base_tip.dart';

class URLOpenUtils {
  /// 腾讯地图调用
  static Future<bool> openTencentMap(double longitude, double latitude, {String address, bool showErr: true}) async {
    String url = 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=FN4BZ-6E33P-LFTDB-VRZ4C-NTP3Z-RVFFK&debug=true&to=${address ?? ''}';
    if (Platform.isIOS)
      url = Uri.encodeFull(url);
    try {
      if (await canLaunch(url) != null) {
        await launch(url);
      }
    } on Exception catch(e) {
      if (showErr)
        showToastCommon('无法调起腾讯地图');
      return false;
    }
    return true;
  }
  /// 高德地图调用
  static Future<bool> openAmap(double longitude, double latitude, {String address, bool showErr: true}) async {
    String url = '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2&poiname=${address ?? ''}';
    if (Platform.isIOS)
      url = Uri.encodeFull(url);
    try {
      if (await canLaunch(url) != null) {
        await launch(url);
      }
    } on Exception catch(e) {
      if (showErr)
        showToastCommon('无法调起高德地图');
      return false;
    }
    return true;
  }

  /// 百度地图
  static Future<bool> openBaiduMap(double longitude, double latitude, {String address, bool showErr: true}) async {
    String url = 'baidumap://map/direction?destination=name:${address ?? ''}|latlng:$latitude,$longitude&coord_type=bd09ll&mode=driving';
    if (Platform.isIOS)
      url = Uri.encodeFull(url);
    try {
      if (await canLaunch(url) != null) {
        await launch(url);
      }
    } on Exception catch(e) {
      if (showErr)
        showToastCommon('无法调起高德地图');
      return false;
    }
    return true;
  }

  /// 苹果地图
  static Future<bool> openAppleMap(longitude, latitude, {String address, bool showErr: true}) async {
    String url = 'http://maps.apple.com/?daddr=$latitude,$longitude&address=$address';
    if (Platform.isIOS)
      url = Uri.encodeFull(url);
    try {
      if (await canLaunch(url) != null) {
        await launch(url);
      }
    } on Exception catch(e) {
      if (showErr)
        showToastCommon('无法调起高德地图');
      return false;
    }
    return true;
  }

  static Future<bool> openMap(longitude, latitude, {String address, bool showErr: false}) async {
    bool flag = true;
    if (!await openAmap(longitude, latitude, address: address, showErr: showErr)) {
      if (!await openBaiduMap(longitude, latitude, address: address, showErr: showErr)) {
        if (!await openTencentMap(longitude, latitude, address: address, showErr: showErr)) {
          // if (!await openAppleMap(longitude, latitude, address: address, showErr: showErr)) {
            flag = false;
            showToastCommon('无法调用地图应用,请安装高德,百度,腾讯任意一种地图应用');
          // }
        }
      }
    }
    return flag;
  }
}

顺便提一下封装成工具类的好处

  1. 减少大量的重复的代码
  2. 当这个第三方插件出现问题或者停止维护时,方便全局替换

其中 longitude代表经度,latitude代表纬度,address代表地理位置
这个工具类兼容了市面上主流了的地图打开方式,适用于Android iOS两大平台

补充说明

为了方便大家测试,给大家推荐一个网站 地图经纬度网站
这个网站可以根据地点获取经纬度
效果图片

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值