lyn4ever-gaode.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //高德key
  2. const key = 'c6a3302bff3ffc7462fa6eb69c3f002e';
  3. const amapFile = require('./amap-uni.js');
  4. const icon = "/static/home/icon-yb.png";
  5. /*
  6. 调用高德地图api进行路线规划时,
  7. 高德对途经点的坐标格式如下:
  8. 116.441063,39.91903;
  9. 116.39622,39.912057;
  10. 116.39622,39.912057;
  11. 116.39622,39.912057;
  12. 116.39622,39.912057;
  13. 116.39622,39.912057;
  14. 116.39622,39.912057;
  15. 116.39622,39.912057
  16. 也就是说每个点的经纬度之间用;分隔,所以请提前格式化好您的坐标格式
  17. */
  18. function getWeather(callback) {
  19. var myAmapFun = new amapFile.AMapWX({
  20. key: key
  21. });
  22. myAmapFun.getWeather({
  23. // city : "天河区",
  24. success: function(data) {
  25. callback(data);
  26. },
  27. fail: function(info) {
  28. //失败回调
  29. uni.$u.toast("获取天气失败,请确保打开定位权限");
  30. }
  31. })
  32. }
  33. function PlanningRoute(start, end, _waypoints, result, fail) {
  34. let that = this;
  35. var myAmapFun = new amapFile.AMapWX({
  36. key: key
  37. });
  38. myAmapFun.getDrivingRoute({
  39. origin: start,
  40. destination: end,
  41. waypoints: _waypoints,
  42. success: function(data) {
  43. console.log(data);
  44. var points = [];
  45. if (data.paths && data.paths[0] && data.paths[0].steps) {
  46. var steps = data.paths[0].steps;
  47. for (var i = 0; i < steps.length; i++) {
  48. var poLen = steps[i].polyline.split(';');
  49. for (var j = 0; j < poLen.length; j++) {
  50. points.push({
  51. longitude: parseFloat(poLen[j].split(',')[0]),
  52. latitude: parseFloat(poLen[j].split(',')[1])
  53. })
  54. }
  55. }
  56. }
  57. //这个返回结果就是对应的路线坐标,其他属性页面自己配置,请参照uniapp地图组件一章节
  58. //https://uniapp.dcloud.io/component/map
  59. result({
  60. points: points,
  61. color: "#289ae6",
  62. width: 12,
  63. arrowLine: true
  64. })
  65. },
  66. fail: function(info) {
  67. fail(info)
  68. }
  69. })
  70. }
  71. //标记标记点
  72. function Makemarkers(startpoi, endpoi, waypoints, success) {
  73. const id = new Date().getTime(); // 拿时间做下id
  74. let markers = [];
  75. //起点
  76. let start = {
  77. iconPath: icon,
  78. id: id,
  79. longitude: startpoi.split(",")[0],
  80. latitude: startpoi.split(",")[1],
  81. width: 28,
  82. height: 34
  83. }
  84. markers.push(start);
  85. let _waypoints = waypoints.split(';');
  86. if (waypoints) {
  87. //途经点,先将其分隔成为数组
  88. for (let i = 0, _len = _waypoints.length; i < _len; i++) {
  89. let point = {
  90. iconPath: icon,
  91. id: id + i + 1,
  92. longitude: parseFloat(_waypoints[i].split(",")[0]),
  93. latitude: parseFloat(_waypoints[i].split(",")[1]),
  94. width: 28,
  95. height: 34
  96. }
  97. markers.push(point)
  98. }
  99. }
  100. if (endpoi) {
  101. //终点
  102. let end = {
  103. iconPath: icon,
  104. id: id + _waypoints.length + 1,
  105. longitude: endpoi.split(",")[0],
  106. latitude: endpoi.split(",")[1],
  107. width: 28,
  108. height: 34
  109. }
  110. markers.push(end)
  111. }
  112. //统一风格为回调方式,也可以直接返回这个markers
  113. success(markers);
  114. }
  115. module.exports = {
  116. line: PlanningRoute,
  117. markers: Makemarkers,
  118. getWeather : getWeather,
  119. icon
  120. }