YSMap.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:url_launcher/url_launcher.dart';
  4. class XMapNavigatorUtil {
  5. /// 高德地图导航
  6. static Future<bool> gotoAMap(
  7. {longitude, latitude, VoidCallback toInstallCallBack}) {
  8. var url =
  9. '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';
  10. return gotoMap(
  11. url: url,
  12. toInstallCallBack: () {
  13. if (null != toInstallCallBack) {
  14. toInstallCallBack();
  15. }
  16. });
  17. }
  18. /// 腾讯地图导航
  19. static Future<bool> gotoTencentMap(
  20. {longitude, latitude, VoidCallback toInstallCallBack}) async {
  21. var url =
  22. 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
  23. return gotoMap(
  24. url: url,
  25. toInstallCallBack: () {
  26. if (null != toInstallCallBack) {
  27. toInstallCallBack();
  28. }
  29. });
  30. }
  31. /// 百度地图导航
  32. static Future<bool> gotoBaiduMap(
  33. {longitude, latitude, VoidCallback toInstallCallBack}) async {
  34. var url =
  35. 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=gcj02&mode=driving';
  36. return gotoMap(
  37. url: url,
  38. toInstallCallBack: () {
  39. if (null != toInstallCallBack) {
  40. toInstallCallBack();
  41. }
  42. });
  43. }
  44. /// 跳转到第三方地图
  45. /// [url]跳转地址
  46. /// [toInstallCallBack]地图未安装回调
  47. static Future<bool> gotoMap(
  48. {String url, VoidCallback toInstallCallBack}) async {
  49. bool canLaunchUrl = await isMapInstall(url);
  50. if (!canLaunchUrl) {
  51. if (null != toInstallCallBack) {
  52. toInstallCallBack();
  53. }
  54. return false;
  55. }
  56. await launch(url);
  57. return true;
  58. }
  59. static void toInstallMap(String url) {
  60. launch(url);
  61. }
  62. static Future<bool> isBaiduMapInstall() {
  63. return canLaunch('baidumap://map/direction');
  64. }
  65. static Future<bool> isTencentMapInstall() {
  66. return canLaunch('qqmap://map/routeplan');
  67. }
  68. static Future<bool> isAmapMapInstall() {
  69. return canLaunch('${Platform.isAndroid ? 'android' : 'ios'}amap://navi');
  70. }
  71. /// 判断地图是否有安装
  72. static Future<bool> isMapInstall(String url) {
  73. return canLaunch(url);
  74. }
  75. }