k25_nearestPointOnLine.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  6. <meta name="author" content="火星科技 http://mars3d.cn " />
  7. <meta name="apple-touch-fullscreen" content="yes" />
  8. <meta name="apple-mobile-web-app-capable" content="yes" />
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  10. <meta name="format-detection" content="telephone=no" />
  11. <meta name="x5-fullscreen" content="true" />
  12. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  13. <!-- 标题及搜索关键字 -->
  14. <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  15. <meta
  16. name="description"
  17. content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型 gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包 地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
  18. />
  19. <link rel="shortcut icon" type="image/x-icon" href="" />
  20. <title>功能示例 </title>
  21. <!--第三方lib-->
  22. <script
  23. type="text/javascript"
  24. src="../lib/include-lib.js"
  25. libpath="../lib/"
  26. include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars3d"
  27. ></script>
  28. <link href="css/style.css" rel="stylesheet" />
  29. </head>
  30. <body class="dark">
  31. <!--加载前进行操作提示,优化用户体验-->
  32. <div id="mask" class="signmask" onclick="removeMask()"></div>
  33. <div id="mars3dContainer" class="mars3d-container"></div>
  34. <!-- 面板 -->
  35. <div class="infoview">
  36. <button class="btn btn-primary" id="btn_drawLine" type="button">绘制线</button>
  37. <button class="btn btn-primary" id="btn_drawPoint" type="button">计算最近点</button>
  38. <button class="btn btn-danger" onclick="clearLayer()" type="button">清除</button>
  39. </div>
  40. <script src="./js/common.js"></script>
  41. <script type="text/javascript">
  42. "use script"; //开发环境建议开启严格模式
  43. var map;
  44. var graphicLayer;
  45. var pointLayer;
  46. function initMap(options) {
  47. //合并属性参数,可覆盖config.json中的对应配置
  48. var mapOptions = mars3d.Util.merge(options, {
  49. scene: {
  50. center: { lat: 31.871794, lng: 116.800468, alt: 57020, heading: 90, pitch: -51 },
  51. fxaa: true,
  52. },
  53. });
  54. //创建三维地球场景
  55. map = new mars3d.Map("mars3dContainer", mapOptions);
  56. //创建矢量数据图层
  57. graphicLayer = new mars3d.layer.GraphicLayer();
  58. map.addLayer(graphicLayer);
  59. // 点矢量数据图层
  60. pointLayer = new mars3d.layer.GraphicLayer();
  61. map.addLayer(pointLayer);
  62. $("#btn_drawLine").click(function () {
  63. if (pointLayer) {
  64. pointLayer.clear();
  65. }
  66. graphicLayer.clear();
  67. graphicLayer.startDraw({
  68. type: "polyline",
  69. style: {
  70. color: "#55ff33",
  71. width: 3,
  72. clampToGround: true,
  73. },
  74. success: function (graphic) {},
  75. });
  76. });
  77. $("#btn_drawPoint").click(function () {
  78. pointLayer.clear();
  79. pointLayer.startDraw({
  80. type: "point",
  81. style: {
  82. pixelSize: 10,
  83. color: "red",
  84. },
  85. success: function (graphic) {
  86. nearPoint();
  87. },
  88. });
  89. });
  90. }
  91. //最近点计算
  92. function nearPoint() {
  93. let lineLayer = graphicLayer.getGraphics();
  94. let point = pointLayer.getGraphics();
  95. if (lineLayer.length < 1 || point.length < 1) {
  96. return;
  97. }
  98. var line = lineLayer[0].toGeoJSON();
  99. var pt = point[0].toGeoJSON();
  100. var snapped = turf.nearestPointOnLine(line, pt, { units: "miles" });
  101. var position = snapped.geometry.coordinates;
  102. //最近点(图标点)
  103. let primitive = new mars3d.graphic.BillboardPrimitive({
  104. position: position,
  105. style: {
  106. image: "img/marker/mark3.png",
  107. scale: 1,
  108. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  109. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  110. scaleByDistance: new Cesium.NearFarScalar(10000, 1.0, 500000, 0.1),
  111. clampToGround: false,
  112. },
  113. popup: "最近点",
  114. });
  115. pointLayer.addGraphic(primitive);
  116. }
  117. //清除数据
  118. function clearLayer() {
  119. graphicLayer.clear();
  120. pointLayer.clear();
  121. }
  122. </script>
  123. </body>
  124. </html>