Flutter 调用地图软件(高德、百度、腾讯、苹果)

一、背景

flutter项目中有去调用其他第三方地图应用的关键字的路线搜索功能,故此做一个总结。

二、使用

1、添加插件:

url_launcher: ^5.4.2

2、iOS 配置 info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
	<string>iosamap</string>
	<string>qqmap</string>
	<string>baidumap</string>
</array>

3、编写工具类
说明一下,通过关键字去搜索地图路线的功能测试已经可以了,地图打开优先级:高德>百度>腾讯>IOS地图,安卓浏览器打开高德h5网页。通过经纬度去进行路线导航,在百度地图中导航需要将高德地图的经纬度转行成百度的经纬度才能使用,转换的逻辑需要自己处理。

import 'dart:io';
import 'package:directsaleapp/common/local/local_storage.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:url_launcher/url_launcher.dart';

class MapUtil {

  /// 苹果地图
  static Future<bool> gotoAppleMapByKeywords(keywords, longitude, latitude) async {
    var url = 'http://maps.apple.com/?&daddr=${Uri.encodeComponent(keywords)}';
    bool canLaunchUrl = await canLaunch(url);
    if (!canLaunchUrl) {
      gotoAppStore();
      return false;
    }
    await launch(url);
  }

  /// 苹果地图
  static Future<bool> gotoAppStore() async {
    var url = 'itms-apps://itunes.apple.com/cn/app/id';
    bool canLaunchUrl = await canLaunch(url);
    if (!canLaunchUrl) {
      Fluttertoast.showToast(msg: '打开失败~', gravity: ToastGravity.CENTER);
      return false;
    }
    await launch(url);
  }

  /// 高德地图
  static Future<bool> gotoAMapByKeywords(keywords, longitude, latitude) async {
    var url =  Platform.isAndroid ? 'androidamap://keywordNavi?sourceApplication=softname&keyword=${Uri.encodeComponent(keywords)}&style=2' :
      'iosamap://path?sourceApplication=applicationName&dname=${Uri.encodeComponent(keywords)}&dev=0&t=0';
    bool canLaunchUrl = await canLaunch(url);
    if (!canLaunchUrl) {
      return gotoBaiduMapByKeywords(keywords, longitude, latitude);
    }
    await launch(url);
    return true;
  }

  /// 百度地图
  static Future<bool> gotoBaiduMapByKeywords(keywords, longitude, latitude) async {
    var url = 'baidumap://map/direction?destination=${Uri.encodeComponent(keywords)}&coord_type=bd09ll&mode=driving';
    bool canLaunchUrl = await canLaunch(url);
    if (!canLaunchUrl) {
      return gotoTencentMapByKeyWords(keywords, longitude, latitude);
    }
    await launch(url);
    return canLaunchUrl;
  }

  /// 腾讯地图
  static Future<bool> gotoTencentMapByKeyWords(keywords, longitude, latitude) async {
    var url = 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&to=${Uri.encodeComponent(keywords)}&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
    bool canLaunchUrl = await canLaunch(url);
    if (!canLaunchUrl) {
      if (Platform.isIOS) {
        return gotoAppleMapByKeywords(keywords, longitude, latitude);
      } else {
        return gotoWebMap(keywords, longitude, latitude);
      }
    }
    await launch(url);
    return canLaunchUrl;
  }

  static Future<bool> gotoWebMap(keywords, longitude, latitude) async {
    var location = await LocalStorage.getMap('location') ?? {};
    var url = 'https://gaode.com/dir?from[name]=${Uri.encodeComponent(location['address'])}&from[lnglat]=${location['longitude']},${location['latitude']}&to[name]=${Uri.encodeComponent(keywords)}&to[lnglat]=$longitude,$latitude&policy=1&type=car';
    bool canLaunchUrl = await canLaunch(url);
    if (!canLaunchUrl) {
      return false;
    }
    await launch(url);
    return canLaunchUrl;
  }


  /// 高德地图
  static Future<bool> gotoAMap(longitude, latitude) async {
    var url = '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';

    bool canLaunchUrl = await canLaunch(url);

    if (!canLaunchUrl) {
      return gotoBaiduMap(longitude, latitude);
//      Fluttertoast.showToast(msg: '未检测到高德地图~');
//      return false;
    }

    await launch(url);

    return true;
  }

  /// 腾讯地图
  static Future<bool> gotoTencentMap(longitude, latitude) async {
    var url = 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
    bool canLaunchUrl = await canLaunch(url);

    if (!canLaunchUrl) {
      if (Platform.isIOS) {
        return gotoAppleMap(longitude, latitude);
      } else {
        Fluttertoast.showToast(msg: '未检测到地图应用~', gravity: ToastGravity.CENTER);
        return false;
      }
    }

    await launch(url);

    return canLaunchUrl;
  }

  /// 百度地图
  static Future<bool> gotoBaiduMap(longitude, latitude) async {
    var url = 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=bd09ll&mode=driving';

    bool canLaunchUrl = await canLaunch(url);

    if (!canLaunchUrl) {
      return gotoTencentMap(longitude, latitude);
//      Fluttertoast.showToast(msg: '未检测到地图应用~');
//      return false;
    }

    await launch(url);

    return canLaunchUrl;
  }

  /// 苹果地图
  static Future<bool> gotoAppleMap(longitude, latitude) async {
    var url = 'http://maps.apple.com/?&daddr=$latitude,$longitude';
    bool canLaunchUrl = await canLaunch(url);
    if (!canLaunchUrl) {
      Fluttertoast.showToast(msg: '打开失败~', gravity: ToastGravity.CENTER);
      return false;
    }
    await launch(url);
  }
}

三、引用

高德地图api
腾讯地图api
百度地图api
苹果地图api
https://blog.csdn.net/haha223545/article/details/105274114/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值