map_navigator_util.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:url_launcher/url_launcher.dart';
  4. /*
  5. * 地图App的跳转,根据launchUrl的方式跳转到对应App导航页面
  6. */
  7. class MapNavigatorUtil {
  8. /// 苹果地图
  9. static Future<bool> gotoAppleMapByKeywords({keywords,longitude, latitude ,required VoidCallback toInstallCallBack}) {
  10. var url = 'http://maps.apple.com/?&daddr=${Uri.encodeComponent(keywords)}';
  11. return gotoMap(url: url, toInstallCallBack: () {
  12. if (null != toInstallCallBack) {
  13. toInstallCallBack();
  14. }
  15. });
  16. // bool canLaunchUrl = await canLaunch(url);
  17. // if (!canLaunchUrl) {
  18. // gotoAppStore();
  19. // return false;
  20. // }
  21. // await launch(url);
  22. //
  23. // return canLaunchUrl;
  24. }
  25. /// 苹果地图
  26. // static Future<bool> gotoAppStore() {
  27. // var url = 'itms-apps://itunes.apple.com/cn/app/id';
  28. // bool canLaunchUrl = await canLaunch(url);
  29. // if (!canLaunchUrl) {
  30. // Fluttertoast.showToast(msg: '打开失败~', gravity: ToastGravity.CENTER);
  31. // return false;
  32. // }
  33. // await launch(url);
  34. // }
  35. /// 苹果地图
  36. static Future<bool> gotoAppleMap({longitude, latitude ,required VoidCallback toInstallCallBack}) {
  37. var url = 'http://maps.apple.com/?&daddr=$latitude,$longitude';
  38. return gotoMap(url: url, toInstallCallBack: () {
  39. if (null != toInstallCallBack) {
  40. toInstallCallBack();
  41. }
  42. });
  43. // bool canLaunchUrl = await canLaunch(url);
  44. // if (!canLaunchUrl) {
  45. // Fluttertoast.showToast(msg: '打开失败~', gravity: ToastGravity.CENTER);
  46. // return false;
  47. // }
  48. // await launch(url);
  49. }
  50. /// 高德地图导航
  51. static Future<bool> gotoAMap(
  52. {longitude, latitude, address, required VoidCallback toInstallCallBack}) {
  53. var url =
  54. // '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';
  55. '${Platform.isAndroid ? 'android' : 'ios'}amap://viewMap?sourceApplication=amap&poiname=${address}&lat=$latitude&lon=$longitude&dev=0';
  56. // '${Platform.isAndroid ? 'android' : 'ios'}amap://viewReGeo?sourceApplication=softname&lat=$latitude&lon=$longitude&dev=0';
  57. return gotoMap(
  58. url: url,
  59. toInstallCallBack: () {
  60. if (null != toInstallCallBack) {
  61. toInstallCallBack();
  62. }
  63. });
  64. }
  65. /// 腾讯地图导航
  66. static Future<bool> gotoTencentMap(
  67. {longitude, latitude,address, required VoidCallback toInstallCallBack}) async {
  68. var url =
  69. //OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
  70. // 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
  71. // 'qqmap://map/marker?marker=coord:$latitude,$longitude;title:marker;addr:汉口火车站&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77';
  72. 'qqmap://map/marker?marker=coord:$latitude,$longitude;title:${address};addr:${address}&referer=BVWBZ-DBB3J-NZGFF-XUKHP-DTDQK-2BFLN';
  73. // 'qqmap://map/geocoder?coord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
  74. return gotoMap(
  75. url: url,
  76. toInstallCallBack: () {
  77. if (null != toInstallCallBack) {
  78. toInstallCallBack();
  79. }
  80. });
  81. }
  82. /// 百度地图导航
  83. static Future<bool> gotoBaiduMap(
  84. {longitude, latitude,address, required VoidCallback toInstallCallBack}) async {
  85. var url =
  86. // 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=gcj02&mode=driving';
  87. // 'baidumap://map/geocoder?location=$latitude,$longitude&coord_type=gcj02&src=andr.baidu.openAPIdemo';
  88. // 'baidumap://map/geocoder?&src=andr.baidu.openAPIdemo&address=湖北省武汉市汉口站';
  89. 'baidumap://map/marker?location=$latitude,$longitude&title=${address}&coord_type=gcj02&src=${Platform.isAndroid ? 'andr' : 'ios'}.baidu.openAPIdemo';
  90. // 'baidumap://map/marker?location=$latitude,$longitude&coord_type=gcj02&src=${Platform.isAndroid ? 'andr' : 'ios'}.baidu.openAPIdemo';
  91. return gotoMap(
  92. url: url,
  93. toInstallCallBack: () {
  94. if (null != toInstallCallBack) {
  95. toInstallCallBack();
  96. }
  97. });
  98. }
  99. /// 跳转到第三方地图
  100. /// [url]跳转地址
  101. /// [toInstallCallBack]地图未安装回调
  102. static Future<bool> gotoMap(
  103. {required String url, required VoidCallback toInstallCallBack}) async {
  104. bool canLaunchUrl = await isMapInstall(url);
  105. if (!canLaunchUrl) {
  106. if (null != toInstallCallBack) {
  107. toInstallCallBack();
  108. }
  109. return false;
  110. }
  111. await launch(url);
  112. return true;
  113. }
  114. static void toInstallMap(String url) {
  115. launch(url);
  116. }
  117. static Future<bool> isBaiduMapInstall() {
  118. return canLaunch('baidumap://map/direction');
  119. }
  120. static Future<bool> isTencentMapInstall() {
  121. return canLaunch('qqmap://map/routeplan');
  122. }
  123. static Future<bool> isAmapMapInstall() {
  124. return canLaunch('${Platform.isAndroid ? 'android' : 'ios'}amap://navi');
  125. }
  126. static Future<bool> isIOSAmapMapInstall() async {
  127. // var url = 'http://maps.apple.com/?&daddr=$latitude,$longitude';
  128. var url = 'http://maps.apple.com/?&daddr=30.617915,114.255173';
  129. bool canLaunchUrl = await canLaunch(url);
  130. return canLaunchUrl;
  131. }
  132. /// 判断地图是否有安装
  133. static Future<bool> isMapInstall(String url) {
  134. return canLaunch(url);
  135. }
  136. }