YSChooseMapView.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:url_launcher/url_launcher_string.dart';
  4. import '../../tool/YSNetWork.dart';
  5. import '../../tool/YSTools.dart';
  6. class YSChooseMapView extends StatelessWidget {
  7. final String latitude;
  8. final String longitude;
  9. const YSChooseMapView({Key? key, required this.latitude, required this.longitude}) : super(key: key);
  10. @override
  11. Widget build(BuildContext context) {
  12. return Container(
  13. height: hsp(220),
  14. width: MediaQuery.of(context).size.width,
  15. decoration: const BoxDecoration(
  16. color: Color(0xFFF1F2F3),
  17. borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10))
  18. ),
  19. child: ListView.separated(
  20. itemBuilder: (context,index){
  21. return GestureDetector(
  22. onTap: (){
  23. if(index==0){
  24. XMapNavigatorUtil.gotoAMap(latitude: latitude,longitude: longitude,toInstallCallBack: (){
  25. ysFlutterToast('高德地图未安装');
  26. // launchUrlString('https://wap.amap.com/?type=pcm&from=banner&src=pc');
  27. });
  28. }else if(index==1){
  29. XMapNavigatorUtil.gotoTencentMap(latitude: latitude,longitude: longitude,toInstallCallBack: (){
  30. ysFlutterToast( '腾讯地图未安装');
  31. // launchUrlString('https://map.qq.com/mobile/');
  32. });
  33. }else if(index==2){
  34. XMapNavigatorUtil.gotoBaiduMap(latitude: latitude,longitude: longitude,toInstallCallBack: (){
  35. ysFlutterToast('百度地图未安装');
  36. // launchUrlString('https://map.baidu.com/mapclient-pages/download/?from=pcmap&source=index');
  37. });
  38. }else{
  39. Navigator.pop(context);
  40. }
  41. },
  42. behavior: HitTestBehavior.opaque,
  43. child: Container(
  44. height: hsp(49),
  45. color: Colors.white,
  46. alignment: Alignment.center,
  47. child: Text(index==0?'高德地图':index==1?'腾讯地图':index==2?'百度地图':'取消'),
  48. ),
  49. );
  50. },
  51. separatorBuilder: (context,index){
  52. return Container(height: index==2?hsp(21):hsp(1),);
  53. },
  54. itemCount: 4
  55. ),
  56. );
  57. }
  58. }
  59. class XMapNavigatorUtil {
  60. /// 高德地图导航
  61. static Future<bool> gotoAMap({longitude, latitude, required VoidCallback toInstallCallBack}) {
  62. var url = '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';
  63. LogUtil.d(url);
  64. return gotoMap(
  65. url: url,
  66. toInstallCallBack: () {
  67. toInstallCallBack();
  68. });
  69. }
  70. /// 腾讯地图导航
  71. static Future<bool> gotoTencentMap(
  72. {longitude, latitude, required VoidCallback toInstallCallBack}) async {
  73. var url = 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
  74. return gotoMap(
  75. url: url,
  76. toInstallCallBack: () {
  77. toInstallCallBack();
  78. });
  79. }
  80. /// 百度地图导航
  81. static Future<bool> gotoBaiduMap(
  82. {longitude, latitude, required VoidCallback toInstallCallBack}) async {
  83. var url = 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=gcj02&mode=driving';
  84. return gotoMap(
  85. url: url,
  86. toInstallCallBack: () {
  87. toInstallCallBack();
  88. });
  89. }
  90. /// 跳转到第三方地图
  91. /// [url]跳转地址
  92. /// [toInstallCallBack]地图未安装回调
  93. static Future<bool> gotoMap({required String url, required VoidCallback toInstallCallBack}) async {
  94. bool canLaunchUrl = true;//await isMapInstall(url);
  95. if (!canLaunchUrl) {
  96. toInstallCallBack();
  97. return false;
  98. }
  99. try{
  100. await launchUrlString(url);
  101. return true;
  102. }catch(e){
  103. toInstallCallBack();
  104. return false;
  105. }
  106. }
  107. static void toInstallMap(String url) {
  108. launchUrlString(url);
  109. }
  110. static Future<bool> isBaiduMapInstall() {
  111. return canLaunchUrlString('baidumap://map/direction');
  112. }
  113. static Future<bool> isTencentMapInstall() {
  114. return canLaunchUrlString('qqmap://map/routeplan');
  115. }
  116. static Future<bool> isAmapMapInstall() {
  117. return canLaunchUrlString('${Platform.isAndroid ? 'android' : 'ios'}amap://navi');
  118. }
  119. /// 判断地图是否有安装
  120. static Future<bool> isMapInstall(String url) {
  121. if(url.contains('amap')){
  122. return isAmapMapInstall();
  123. }else if(url.contains('qqmap')){
  124. return isBaiduMapInstall();
  125. }else if(url.contains('baidumap')){
  126. return isBaiduMapInstall();
  127. }
  128. return canLaunchUrlString(url);
  129. }
  130. }