map.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  7. <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
  8. <title>运输轨迹</title>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. html,
  15. body,
  16. #container {
  17. height: 100%;
  18. width: 100%;
  19. }
  20. .content{
  21. background-color: white;
  22. position: absolute;
  23. top: 20px;
  24. padding: 6px 10px;
  25. border-radius: 6px;
  26. width: 90%;
  27. margin-left: 5%;
  28. display: flex;
  29. align-items: center;
  30. }
  31. .start,.end{
  32. display: flex;
  33. }
  34. .distance {
  35. background-color: #00000061;
  36. position: absolute;
  37. bottom: 20px;
  38. right: 20px;
  39. color: white;
  40. padding: 6px 10px;
  41. border-radius: 6px;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="container"></div>
  47. <div class='content'>
  48. <div class='start'>
  49. <div id='sendCity'></div>
  50. <div id='sendArea'></div>
  51. </div>
  52. <div>-----------></div>
  53. <div class='end'>
  54. <div id='unloadCity'></div>
  55. <div id='unloadArea'></div>
  56. </div>
  57. </div>
  58. <div id="distance" class='distance'></div>
  59. <script
  60. src="https://webapi.amap.com/maps?v=2.0&key=211dd6f989e719022aaf47ddb0659c47&plugin=AMap.Scale,AMap.ToolBar,AMap.Geocoder,AMap.Geolocation,Geolocation,AMap.Driving">
  61. </script>
  62. <script src="https://webapi.amap.com/loca?v=2.0.0&key=211dd6f989e719022aaf47ddb0659c47"></script>
  63. <script>
  64. var _t = getQueryString("obj");
  65. var obj = JSON.parse(decodeURI(_t)) ;
  66. console.log('---------------------')
  67. console.log(obj)
  68. document.getElementById('sendCity').innerHTML = obj.sendCity
  69. document.getElementById('sendArea').innerHTML = obj.sendArea
  70. document.getElementById('unloadCity').innerHTML = obj.unloadCity
  71. document.getElementById('unloadArea').innerHTML = obj.unloadArea
  72. function getQueryString(name) {
  73. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  74. var r = window.location.search.substr(1).match(reg);
  75. if (r != null) return unescape(r[2]); return null;
  76. }
  77. let starLnglat = [40.243655, 122.114407]
  78. console.log(starLnglat)
  79. var map = new AMap.Map('container', {
  80. zoom: 12,
  81. center: [starLnglat[1], starLnglat[0]],
  82. resizeEnable: true
  83. });
  84. map.clearMap();
  85. var startIcon = new AMap.Icon({
  86. size: new AMap.Size(25, 25),
  87. image: './img/1.png',
  88. imageSize: new AMap.Size(25, 25),
  89. });
  90. /*
  91. * 驾车策略
  92. * AMap.DrivingPolicy.LEAST_TIME 最快捷模式
  93. * AMap.DrivingPolicy.LEAST_FEE 最经济模式
  94. * AMap.DrivingPolicy.LEAST_DISTANCE 最短距离模式
  95. * AMap.DrivingPolicy.REAL_TRAFFIC 考虑实时路况
  96. */
  97. var drivingOption = {
  98. policy: AMap.DrivingPolicy
  99. .LEAST_TIME, // 其它policy参数请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingPolicy
  100. ferry: 1, // 是否可以使用轮渡
  101. map: map,
  102. hideMarkers: false, // 设置隐藏路径规划的起始点图标
  103. autoFitView: true
  104. }
  105. var dis = getDistance(122.114407, 40.243655, 122.114407, 42.243655)
  106. document.getElementById("distance").innerHTML = dis
  107. console.log(dis)
  108. render(122.114407, 42.243655)
  109. function getDistance(lat1, lng1, lat2, lng2) {
  110. function Rad(d) {
  111. return d * Math.PI / 180.0;
  112. }
  113. if (!lat1 || !lng1) {
  114. return '';
  115. }
  116. // lat1用户的纬度
  117. // lng1用户的经度
  118. // lat2商家的纬度
  119. // lng2商家的经度
  120. let radLat1 = Rad(lat1);
  121. let radLat2 = Rad(lat2);
  122. let a = radLat1 - radLat2;
  123. let b = Rad(lng1) - Rad(lng2);
  124. let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) *
  125. Math.pow(
  126. Math.sin(b / 2), 2)));
  127. s = s * 6378.137;
  128. s = Math.round(s * 10000) / 10000;
  129. s = '距离' + s.toFixed(2) + '公里' //保留两位小数
  130. return s
  131. }
  132. function render(endLng, endLat) {
  133. // 构造路线导航类
  134. var driving = new AMap.Driving(drivingOption)
  135. // 根据起终点经纬度规划驾车导航路线
  136. driving.search(new AMap.LngLat(starLnglat[1], starLnglat[0]), new AMap.LngLat(endLng, endLat), function(status,
  137. result) {
  138. if (status === 'complete') {
  139. console.log('绘制驾车路线完成')
  140. } else {
  141. console.log('获取驾车数据失败:' + result)
  142. }
  143. });
  144. // var capitals = [{
  145. // center: [116.42, 39.93123],
  146. // }, {
  147. // center: [116.41, 39.92132],
  148. // }, {
  149. // center: [116.40, 39.91122],
  150. // }];
  151. var capitals = [];
  152. var facilities = [];
  153. for (var i = 0; i < capitals.length; i++) {
  154. var marker = new AMap.Marker({
  155. position: new AMap.LngLat(capitals[i].center[0], capitals[i].center[1]),
  156. offset: new AMap.Pixel(-10, -10),
  157. icon: startIcon,
  158. });
  159. facilities.push(marker);
  160. }
  161. map.add(facilities);
  162. }
  163. // map.on('click', clickHandler)
  164. function clickHandler(e) {
  165. map.clearMap();
  166. var endLng = e.lnglat.getLng();
  167. var endLat = e.lnglat.getLat();
  168. render(endLng, endLat)
  169. }
  170. </script>
  171. <script>
  172. </script>
  173. </body>
  174. </html>