map.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. html,
  14. body,
  15. #container {
  16. height: 100%;
  17. width: 100%;
  18. }
  19. .distance {
  20. background-color: #00000061;
  21. position: absolute;
  22. bottom: 20px;
  23. right: 20px;
  24. color: white;
  25. padding: 6px 10px;
  26. border-radius: 6px;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="container"></div>
  32. <div id="distance" class='distance'></div>
  33. <script
  34. src="https://webapi.amap.com/maps?v=2.0&key=211dd6f989e719022aaf47ddb0659c47&plugin=AMap.Scale,AMap.ToolBar,AMap.Geocoder,AMap.Geolocation,Geolocation,AMap.Driving">
  35. </script>
  36. <script src="https://webapi.amap.com/loca?v=2.0.0&key=211dd6f989e719022aaf47ddb0659c47"></script>
  37. <script>
  38. function getQuery(name) {
  39. // 正则:[找寻'&' + 'url参数名字' = '值' + '&']('&'可以不存在)
  40. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  41. let r = window.location.search.substr(1).match(reg);
  42. console.log(r);
  43. if (r != null) {
  44. // 对参数值进行解码
  45. return decodeURIComponent(r[2]);
  46. }
  47. return null;
  48. }
  49. let starLnglat = getQuery('lat').toString().split(',');
  50. let endLnglat = getQuery('unsendaddress').toString().split(',')
  51. console.log(endLnglat,111111111)
  52. // let starLnglat = [40.243655, 122.114407]
  53. console.log('---------------------')
  54. console.log(starLnglat)
  55. var map = new AMap.Map('container', {
  56. zoom: 12,
  57. center: [starLnglat[1], starLnglat[0]],
  58. resizeEnable: true
  59. });
  60. map.clearMap();
  61. var startIcon = new AMap.Icon({
  62. size: new AMap.Size(25, 25),
  63. image: './img/1.png',
  64. imageSize: new AMap.Size(25, 25),
  65. });
  66. /*
  67. * 驾车策略
  68. * AMap.DrivingPolicy.LEAST_TIME 最快捷模式
  69. * AMap.DrivingPolicy.LEAST_FEE 最经济模式
  70. * AMap.DrivingPolicy.LEAST_DISTANCE 最短距离模式
  71. * AMap.DrivingPolicy.REAL_TRAFFIC 考虑实时路况
  72. */
  73. var drivingOption = {
  74. policy: AMap.DrivingPolicy
  75. .LEAST_TIME, // 其它policy参数请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingPolicy
  76. ferry: 1, // 是否可以使用轮渡
  77. map: map,
  78. hideMarkers: false, // 设置隐藏路径规划的起始点图标
  79. autoFitView: true
  80. }
  81. // var dis = getDistance(starLnglat[1], starLnglat[0], endLnglat[1], endLnglat[0])
  82. var dis = '距离'+getQuery('distance')+'公里'
  83. document.getElementById("distance").innerHTML = dis
  84. // console.log(dis)
  85. render(endLnglat[1], endLnglat[0])
  86. // function getDistance(lat1, lng1, lat2, lng2) {
  87. // function Rad(d) {
  88. // return d * Math.PI / 180.0;
  89. // }
  90. // if (!lat1 || !lng1) {
  91. // return '';
  92. // }
  93. // // lat1用户的纬度
  94. // // lng1用户的经度
  95. // // lat2商家的纬度
  96. // // lng2商家的经度
  97. // let radLat1 = Rad(lat1);
  98. // let radLat2 = Rad(lat2);
  99. // let a = radLat1 - radLat2;
  100. // let b = Rad(lng1) - Rad(lng2);
  101. // let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) *
  102. // Math.pow(
  103. // Math.sin(b / 2), 2)));
  104. // s = s * 6378.137;
  105. // s = Math.round(s * 10000) / 10000;
  106. // s = '距离' + s.toFixed(2) + '公里' //保留两位小数
  107. // return s
  108. // }
  109. function render(endLng, endLat) {
  110. // 构造路线导航类
  111. var driving = new AMap.Driving(drivingOption)
  112. // 根据起终点经纬度规划驾车导航路线
  113. driving.search(new AMap.LngLat(starLnglat[1], starLnglat[0]), new AMap.LngLat(endLng, endLat), function(status,
  114. result) {
  115. if (status === 'complete') {
  116. console.log('绘制驾车路线完成')
  117. } else {
  118. console.log('获取驾车数据失败:' + result)
  119. }
  120. });
  121. // var capitals = [{
  122. // center: [116.42, 39.93123],
  123. // }, {
  124. // center: [116.41, 39.92132],
  125. // }, {
  126. // center: [116.40, 39.91122],
  127. // }];
  128. var capitals = [];
  129. var facilities = [];
  130. for (var i = 0; i < capitals.length; i++) {
  131. var marker = new AMap.Marker({
  132. position: new AMap.LngLat(capitals[i].center[0], capitals[i].center[1]),
  133. offset: new AMap.Pixel(-10, -10),
  134. icon: startIcon,
  135. });
  136. facilities.push(marker);
  137. }
  138. map.add(facilities);
  139. }
  140. // map.on('click', clickHandler)
  141. function clickHandler(e) {
  142. map.clearMap();
  143. var endLng = e.lnglat.getLng();
  144. var endLat = e.lnglat.getLat();
  145. render(endLng, endLat)
  146. }
  147. </script>
  148. <script>
  149. </script>
  150. </body>
  151. </html>