map.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>OpenStreetMap 路线规划</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
  8. <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
  9. <script src="./js/lib/common.js"></script>
  10. <style>
  11. html, body, #map {
  12. position: relative;
  13. width: 100%;
  14. height: 100%;
  15. display: flex;
  16. justify-content: center;
  17. align-items: center;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="map" ></div>
  23. </body>
  24. <script type="text/javascript" src="./js/lib/uniwebviewsdk.js"></script>
  25. <script>
  26. // 初始化地图
  27. let state = 0;
  28. state = getQueryVariable('shstate');
  29. let dlat=getQueryVariable('dlat');
  30. let dlng=getQueryVariable('dlng');
  31. const map = L.map('map').setView([dlat, dlng], 13); // 上海坐标
  32. L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  33. maxZoom: 19,
  34. attribution: '&copy; OpenStreetMap'
  35. }).addTo(map);
  36. let markers = [];
  37. let routeLine = null;
  38. // 点击地图添加起点/终点
  39. map.on('click', async (e) => {
  40. const { lat, lng } = e.latlng;
  41. if (markers.length >= 1) {
  42. // 重置
  43. markers.forEach(m => map.removeLayer(m));
  44. markers = [];
  45. if (routeLine) {
  46. map.removeLayer(routeLine);
  47. routeLine = null;
  48. }
  49. }
  50. const marker = L.marker([lat, lng]).addTo(map);
  51. markers.push(marker);
  52. const url = `https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${lat}&lon=${lng}`;
  53. const res = await fetch(url);
  54. const data = await res.json();
  55. if(state!=0){
  56. marker.bindPopup(`📍 当前位置:<br>纬度:${lat}<br>经度:${lng}<br>${data.display_name}`).openPopup();
  57. }
  58. console.log("选择的坐标:", lat, lng);
  59. uni.postMessage({
  60. data: {
  61. data:`${lat};${lng}`, // 回传
  62. type:88
  63. },
  64. });
  65. if (markers.length === 2) {
  66. const start = markers[0].getLatLng();
  67. const end = markers[1].getLatLng();
  68. // 请求 OSRM 路线 API
  69. const url = `https://router.project-osrm.org/route/v1/driving/${start.lng},${start.lat};${end.lng},${end.lat}?overview=full&geometries=geojson`;
  70. const response = await fetch(url);
  71. const json = await response.json();
  72. if (json.code === 'Ok') {
  73. const coords = json.routes[0].geometry.coordinates.map(c => [c[1], c[0]]);
  74. routeLine = L.polyline(coords, { color: 'blue', weight: 5 }).addTo(map);
  75. map.fitBounds(routeLine.getBounds());
  76. const distance = (json.routes[0].distance / 1000).toFixed(2);
  77. const duration = (json.routes[0].duration / 60).toFixed(1);
  78. alert(`路线规划完成:\n距离:${distance} 公里\n预计时间:${duration} 分钟`);
  79. } else {
  80. alert('路线规划失败');
  81. }
  82. }
  83. });
  84. function showAddrMark (obj) {
  85. if (markers.length >= 1) {
  86. // 重置
  87. markers.forEach(m => map.removeLayer(m));
  88. markers = [];
  89. if (routeLine) {
  90. map.removeLayer(routeLine);
  91. routeLine = null;
  92. }
  93. }
  94. const lat=obj.payload.lat;
  95. const lng = obj.payload.lon;
  96. const marker = L.marker([lat, lng]).addTo(map);
  97. markers.push(marker);
  98. };
  99. </script>
  100. </html>