turf_measurement.html 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8">
  8. <title data-i18n="resources.title_turfMeasurement"></title>
  9. <style>
  10. .editPane {
  11. position: absolute;
  12. right: 10px;
  13. top: 10px;
  14. width: 350px;
  15. text-align: center;
  16. background: #FFF;
  17. z-index: 1000;
  18. }
  19. </style>
  20. </head>
  21. <body style='margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;'>
  22. <div id='map' style='margin:0 auto;width: 100%;height: 100%'></div>
  23. <div class='panel panel-primary editPane' id='editPane'>
  24. <div class='panel-heading'>
  25. <h5 class='panel-title text-center' data-i18n="resources.text_Jingshen"></h5>
  26. </div>
  27. <div class='panel panel-body chooseItems' style="margin-bottom:0px;padding-bottom: 0px">
  28. <select class='form-control' id='menuSelect'>
  29. <option value='along' selected='selected'>along</option>
  30. <option value='area'>area</option>
  31. <option value='bbox'>bbox</option>
  32. <option value='center'>center</option>
  33. <option value='destination'>destination</option>
  34. <option value='distance'>distance</option>
  35. <option value='midpoint'>midpoint</option>
  36. <option value='square'>square</option>
  37. <option value='greatCircle'>greatCircle</option>
  38. <option value='bezierSpline'>bezierSpline</option>
  39. <option value='buffer'>buffer</option>
  40. <option value='circle'>circle</option>
  41. </select>
  42. </div>
  43. <hr/>
  44. <div class='panel-body' id='params' style="padding-top: 0px">
  45. <div class='input-group' id="drawMethods">
  46. <div class="btn-group" role="group" aria-label="..." id="draw">
  47. <button id="drawPoint" value='Point' type="button" class="btn btn-default"
  48. data-i18n="resources.text_input_value_drawPoint">
  49. </button>
  50. <button id="drawPolygon" value='Polygon' type="button" class="btn btn-default"
  51. data-i18n="resources.text_input_value_drawPolygon">
  52. </button>
  53. <button id="none" value='None' type="button" class="btn btn-default" data-i18n="resources.btn_notDraw">
  54. </button>
  55. <button id="clearLayer" value='Clear' type="button" class="btn btn-default"
  56. data-i18n="resources.text_input_value_clear">
  57. </button>
  58. </div>
  59. </div>
  60. <p>
  61. <div class='input-group'>
  62. <span class='input-group-addon' data-i18n="resources.text_distanceUnit"></span>
  63. <select class='form-control' id='unitSelect'>
  64. <option value='kilometers' selected='selected'>kilometers</option>
  65. <option value='miles'>miles</option>
  66. <option value='degrees'>degrees</option>
  67. <option value='radians'>radians</option>
  68. </select>
  69. </div>
  70. <p></p>
  71. <div class='input-group'>
  72. <span class='input-group-addon' data-i18n="resources.text_distanceToStart"></span>
  73. <input id='distance' type='text' class='form-control' data-i18n="[placeholder]resources.text_distance"/>
  74. </div>
  75. <p></p>
  76. <div class='input-group'>
  77. <span class='input-group-addon'></span>
  78. <input id='bearing' type='text' class='form-control' data-i18n="[placeholder]resources.text_positionRange"/>
  79. </div>
  80. <p></p>
  81. <div align='right' class='input-group'>
  82. <input type='button' id='calc' class='btn btn-primary' data-i18n="[value]resources.text_getPoint"/>&nbsp
  83. </div>
  84. </div>
  85. </div>
  86. </body>
  87. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  88. <script type="text/javascript" include="turf" src="../../dist/ol/include-ol.js"></script>
  89. <script>
  90. var host = window.isLocal ? window.server : "https://iserver.supermap.io",
  91. url = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
  92. urlWorld = host + "/iserver/services/map-world/rest/maps/World",
  93. container = document.getElementById('info'), distance, geo, coordinate,
  94. choose, bearing, result,
  95. radius, steps, draw, drawSource,
  96. overlay = new ol.Overlay(({
  97. element: container,
  98. autoPan: true,
  99. autoPanAnimation: {
  100. duration: 250
  101. }
  102. }));
  103. //添加底图
  104. map = new ol.Map({
  105. target: 'map',
  106. controls: ol.control.defaults({attributionOptions: {collapsed: false}})
  107. .extend([new ol.supermap.control.Logo()]),
  108. view: new ol.View({
  109. center: [116.407283, 39.90455699411283],
  110. zoom: 11,
  111. projection: 'EPSG:4326'
  112. }),
  113. overlays: [overlay]
  114. });
  115. var baseLayer = new ol.layer.Tile({
  116. source: new ol.source.TileSuperMapRest({
  117. url: url,
  118. prjCoordSys: {"epsgCode": 4326}
  119. }),
  120. projection: 'EPSG:4326'
  121. });
  122. map.addLayer(baseLayer);
  123. //初始化的图层
  124. var turfSource = new ol.source.Turf({
  125. wrapX: false,
  126. attributions: "© <a href='https://turfjs.org/' target='_blank'>turfjs<span></a> with"
  127. });
  128. map.addLayer(new ol.layer.Vector({
  129. source: turfSource,
  130. style: new ol.style.Style({
  131. stroke: new ol.style.Stroke({
  132. color: 'rgba(0,153,255,1)',
  133. width: 6
  134. }),
  135. image: new ol.style.Icon(({
  136. crossOrigin: 'anonymous',
  137. src: '../img/marker-icon.png',
  138. scale: 0.7
  139. })),
  140. fill: new ol.style.Fill({
  141. color: 'rgba(0,153,255,0.1)'
  142. })
  143. })
  144. }));
  145. //结果图层
  146. var resultSource = new ol.source.Turf({
  147. wrapX: false,
  148. attributions: ""
  149. });
  150. map.addLayer(new ol.layer.Vector({
  151. source: resultSource,
  152. style: new ol.style.Style({
  153. stroke: new ol.style.Stroke({
  154. color: 'rgb(255,0,0)',
  155. width: 4
  156. }),
  157. image: new ol.style.Icon(({
  158. crossOrigin: 'anonymous',
  159. src: '../img/marker-gold.png',
  160. scale: 0.7
  161. })),
  162. fill: new ol.style.Fill({
  163. color: 'rgba(178,34,34,0.3)'
  164. })
  165. }),
  166. }));
  167. //初始化数据
  168. function bindEvents() {
  169. $('.chooseItems').show();
  170. hidePanel();
  171. $('.panel-heading>h5').text('turf测量');
  172. $("#menuSelect").change(function (e) {
  173. //初始化图层
  174. isUseWorldMap(0);
  175. overlay.setPosition(undefined);
  176. clearSource();
  177. resetSelect();
  178. choose = $('#menuSelect').children('option:selected').val();
  179. //对应菜单显示
  180. if (choose === 'along') {
  181. widgets.alert.clearAlert();
  182. rodeCoordinate();
  183. drawALine();
  184. setAlongIcon();
  185. $('.panel-heading>h5').text(resources.text_Jingshen);
  186. map.getView().animate({zoom: 15}, {duration: 500, center: [116.12499999899998, 39.70771500024742]});
  187. $('.input-group:eq(0)').hide();
  188. $('.input-group:eq(1)').show();
  189. $('.input-group:eq(2)').show();
  190. $('.input-group:eq(2)>span').text(resources.text_distanceToStart);
  191. $('.input-group:eq(2)>input').attr('placeholder', resources.text_totalLength).val("0.5");
  192. $('.input-group:eq(3)').hide();
  193. $('.input-group:eq(4)').show();
  194. $('#calc').val(resources.text_getPoint);
  195. }
  196. else if (choose === 'area') {
  197. widgets.alert.clearAlert();
  198. widgets.alert.showAlert(resources.msg_drawPolygonAndClick, true);
  199. drawing();
  200. $('#drawPoint').hide();
  201. $('#drawPolygon').show();
  202. $('.panel-heading>h5').text(resources.text_measureArea);
  203. map.getView().animate({zoom: 11}, {duration: 500, center: [116.407283, 39.90455699411283]});
  204. $('.input-group:eq(0)').show();
  205. $('.input-group:eq(1)').hide();
  206. $('.input-group:eq(2)').hide();
  207. $('.input-group:eq(3)').hide();
  208. $('.input-group:eq(4)').show();
  209. $('#calc').val(resources.text_getArea);
  210. } else if (choose === 'bbox') {
  211. widgets.alert.clearAlert();
  212. rodeCoordinate();
  213. drawALine();
  214. $('.panel-heading>h5').text(resources.text_JingshenBounds);
  215. map.getView().animate({zoom: 16}, {duration: 500, center: [116.12499999899998, 39.70171500024742]});
  216. $('.input-group:eq(0)').hide();
  217. $('.input-group:eq(1)').hide();
  218. $('.input-group:eq(2)').hide();
  219. $('.input-group:eq(3)').hide();
  220. $('.input-group:eq(4)').show();
  221. $('#calc').val(resources.text_getBounds);
  222. }
  223. else if (choose === 'destination') {
  224. //北京
  225. widgets.alert.clearAlert();
  226. setDestinationIcon();
  227. $('.panel-heading>h5').text(resources.text_pointToTarget);
  228. map.getView().animate({zoom: 10}, {duration: 500, center: [116.407283, 39.90455699411283]});
  229. $('.input-group:eq(0)').hide();
  230. $('.input-group:eq(1)').show();
  231. $('.input-group:eq(2)').show();
  232. $('.input-group:eq(3)').show();
  233. $('.input-group:eq(4)').show();
  234. $('.input-group:eq(2)>span').text(resources.text_distanceToStart);
  235. $('.input-group:eq(2)>input').attr('placeholder', resources.text_distanceToStart).val("0.5");
  236. $('.input-group:eq(3)>span').text(resources.text_position);
  237. $('.input-group:eq(3)>input').attr('placeholder', resources.text_positionRange);
  238. $('#calc').val(resources.text_getTargetPoint);
  239. }
  240. else if (choose === 'center') {
  241. widgets.alert.clearAlert();
  242. setCenterIcon();
  243. $('.panel-heading>h5').text(resources.text_scenicSpotsCenter);
  244. map.getView().animate({zoom: 12}, {duration: 500, center: [116.38035099999959, 39.93393099410061]});
  245. $('.input-group:eq(0)').hide();
  246. $('.input-group:eq(1)').hide();
  247. $('.input-group:eq(2)').hide();
  248. $('.input-group:eq(3)').hide();
  249. $('.input-group:eq(4)').show();
  250. $('#calc').val(resources.text_getScenicSpotCenter);
  251. }
  252. else if (choose === 'distance') {
  253. widgets.alert.clearAlert();
  254. setDistanceIcon();
  255. $('.panel-heading>h5').text(resources.text_distanceBetween);
  256. map.getView().animate({zoom: 8}, {duration: 500, center: [116.12499999899998, 39.70771500024742]});
  257. $('.input-group:eq(0)').hide();
  258. $('.input-group:eq(1)').show();
  259. $('.input-group:eq(2)').hide();
  260. $('.input-group:eq(3)').hide();
  261. $('.input-group:eq(4)').show();
  262. $('#calc').val(resources.text_getDistance);
  263. }
  264. else if (choose === 'midpoint') {
  265. widgets.alert.clearAlert();
  266. setMidpointIcon();
  267. $('.panel-heading>h5').text(resources.text_middlePointBetween);
  268. map.getView().animate({zoom: 8}, {duration: 500, center: [116.12499999899998, 39.70771500024742]});
  269. $('.input-group:eq(0)').hide();
  270. $('.input-group:eq(1)').hide();
  271. $('.input-group:eq(2)').hide();
  272. $('.input-group:eq(3)').hide();
  273. $('.input-group:eq(4)').show();
  274. $('#calc').val(resources.text_getMiddlePoint);
  275. }
  276. else if (choose === 'greatCircle') {
  277. widgets.alert.clearAlert();
  278. isUseWorldMap(1);
  279. setGreatCircleIcon();
  280. map.getView().animate({zoom: 4}, {duration: 500, center: [90.407283, 40.90455699411283]});
  281. $('.panel-heading>h5').text(resources.text_bigRoundRoute);
  282. $('.input-group:eq(0)').hide();
  283. $('.input-group:eq(1)').hide();
  284. $('.input-group:eq(2)').hide();
  285. $('.input-group:eq(3)').hide();
  286. $('.input-group:eq(4)').show();
  287. $('#calc').val(resources.text_getRoute);
  288. }
  289. else if (choose === 'buffer') {
  290. widgets.alert.clearAlert();
  291. drawing();
  292. $('#drawPolygon').hide();
  293. $('#drawPoint').show();
  294. widgets.alert.showAlert(resources.msg_markPoint, true);
  295. map.getView().animate({zoom: 12}, {duration: 500, center: [116.407283, 39.90455699411283]});
  296. $('.panel-heading>h5').text(resources.text_buffer);
  297. $('.input-group:eq(0)').show();
  298. $('.input-group:eq(1)').show();//单位
  299. $('.input-group:eq(2)').show();//步长
  300. $('.input-group:eq(3)').show(); //半径
  301. $('.input-group:eq(4)').show();
  302. $('.input-group:eq(3)>span').text(resources.text_radius);//半径
  303. $('.input-group:eq(3)>input').attr('placeholder', resources.text_radius).val("10");
  304. $('.input-group:eq(2)>span').text(resources.text_step);//步长
  305. $('.input-group:eq(2)>input').attr('placeholder', resources.text_step).val("10");
  306. $('#calc').val(resources.text_getBuffer);
  307. }
  308. else if (choose === 'circle') {
  309. widgets.alert.clearAlert();
  310. drawing();
  311. $('#drawPolygon').hide();
  312. $('#drawPoint').show();
  313. widgets.alert.showAlert(resources.msg_markPoint, true);
  314. map.getView().animate({zoom: 12}, {duration: 500, center: [116.407283, 39.90455699411283]});
  315. $('.panel-heading>h5').text(resources.text_polygon);
  316. $('.input-group:eq(0)').show();
  317. $('.input-group:eq(1)').show();//单位
  318. $('.input-group:eq(2)').show();//步长
  319. $('.input-group:eq(3)').show(); //半径
  320. $('.input-group:eq(4)').show();
  321. $('.input-group:eq(3)>span').text(resources.text_radius);//半径
  322. $('.input-group:eq(3)>input').attr('placeholder', resources.text_radius).val("10");
  323. $('.input-group:eq(2)>span').text(resources.text_step);//步长
  324. $('.input-group:eq(2)>input').attr('placeholder', resources.text_step).val("10");
  325. $('#calc').val(resources.text_getPolygon);
  326. }
  327. else if (choose === 'bezierSpline') {
  328. widgets.alert.clearAlert();
  329. $('.panel-heading>h5').text(resources.text_bezier);
  330. map.getView().animate({zoom: 12}, {
  331. duration: 500,
  332. center: [116.55439124389999, 40.676116230453765]
  333. });
  334. $('.input-group:eq(0)').hide();
  335. $('.input-group:eq(1)').hide();//单位
  336. $('.input-group:eq(2)').hide();//步长
  337. $('.input-group:eq(3)').hide(); //半径
  338. $('.input-group:eq(4)').show();
  339. whiteRiver();
  340. setBezierLine();
  341. $('#calc').val(resources.text_getBezier);
  342. }
  343. else if (choose === 'square') {
  344. widgets.alert.clearAlert();
  345. setSquarePolygon();
  346. $('.panel-heading>h5').text(resources.text_externalTangentSquare);
  347. map.getView().animate({zoom: 12}, {duration: 500, center: [116.18865966796875, 39.88904724121094]});
  348. $('.input-group:eq(0)').hide();//draw控件
  349. $('.input-group:eq(1)').hide();//单位
  350. $('.input-group:eq(2)').hide();//距离
  351. $('.input-group:eq(3)').hide(); //步长
  352. $('.input-group:eq(4)').show();
  353. $('#calc').val(resources.text_getExternalTangentSquare);
  354. }
  355. else {
  356. widgets.alert.clearAlert(resources.msg_noMethod, false);
  357. }
  358. showPanel();
  359. });
  360. //生成对应距离点点击事件
  361. $('#calc').click(function () {
  362. //获取自定义距离
  363. distance = $('#distance').val();
  364. //获取自定方位
  365. bearing = $('#bearing').val();
  366. //步长
  367. steps = distance;
  368. //半径
  369. radius = bearing;
  370. //角度
  371. angle = distance;
  372. meths = {
  373. "along": along,
  374. "area": area,
  375. "bbox": bbox,
  376. "destination": destination,
  377. "distance": turf_Distance,
  378. "midpoint": midpoint,
  379. "greatCircle": greatCircle,
  380. "square": square,
  381. "center": center,
  382. "bezierSpline": bezierSpline,
  383. "buffer": buffer,
  384. "circle": circle,
  385. };
  386. if (!meths[choose]) {
  387. widgets.alert.showAlert(resources.msg_illegal, false);
  388. }
  389. meths[choose]();
  390. });
  391. //获取单位
  392. $('#unitSelect').change(function () {
  393. unit = $('#unitSelect').children('option:selected').val();
  394. });
  395. }
  396. bindEvents();
  397. $("#menuSelect").change();
  398. //白河
  399. function whiteRiver() {
  400. coordinate = [
  401. [116.44468018273878, 40.675731250349635],
  402. [116.45764286288912, 40.67514041485476],
  403. [116.47234517561137, 40.675374865714495],
  404. [116.4864209475681, 40.68284917168584],
  405. [116.50806547030137, 40.6926826346566],
  406. [116.51490194761084, 40.701383778180805],
  407. [116.54403108790551, 40.68381460602459],
  408. [116.55439124389999, 40.676116230453765],
  409. [116.56406552077725, 40.67476948501498],
  410. [116.58188801112561, 40.68296164868846],
  411. [116.5925083591713, 40.70683026321353],
  412. [116.59861159022633, 40.72097728359635],
  413. [116.61194828377546, 40.71945202953016],
  414. [116.6157779223366, 40.71459183686403]
  415. ];
  416. }
  417. // 京深路数据
  418. function rodeCoordinate() {
  419. coordinate = [
  420. [116.12499999899998, 39.70771500024742],
  421. [116.124684999625, 39.70738300038022],
  422. [116.124684999625, 39.70738300038022],
  423. [116.12366300000001, 39.70630000081341],
  424. [116.12366300000001, 39.70630000081341],
  425. [116.122695000875, 39.70526299956156],
  426. [116.11948400050001, 39.70182899926848],
  427. [116.11948400050001, 39.70182899926848],
  428. [116.117782000875, 39.70001099999569],
  429. [116.117782000875, 39.70001099999569],
  430. [116.11697300062501, 39.69914100034369]
  431. ];
  432. }
  433. //自定义的一条线
  434. function drawALine() {
  435. turfSource.process("Helper.lineString", {
  436. "coordinates": coordinate,
  437. });
  438. }
  439. function setAlongIcon() {
  440. //添加目的地
  441. var start = new ol.geom.Point([116.12499999899998, 39.70771500024742]);
  442. var end = new ol.geom.Point([116.11697300062501, 39.69914100034369]);
  443. var iconStart = new ol.style.Style({
  444. image: new ol.style.Icon(({
  445. src: '../img/start_trans.png'
  446. }))
  447. });
  448. var iconEnd = new ol.style.Style({
  449. image: new ol.style.Icon(({
  450. src: '../img/end_trans.png'
  451. }))
  452. });
  453. var feature1 = new ol.Feature(start);
  454. var feature2 = new ol.Feature(end);
  455. turfSource.addFeature(feature2);
  456. turfSource.addFeature(feature1);
  457. feature1.setStyle(iconStart);
  458. feature2.setStyle(iconEnd);
  459. }
  460. //判断底图
  461. function isUseWorldMap(bool) {
  462. if (bool) {
  463. baseLayer.setSource(new ol.source.TileSuperMapRest({
  464. url: urlWorld,
  465. prjCoordSys: {"epsgCode": 4326}
  466. }));
  467. }
  468. else {
  469. baseLayer.setSource(new ol.source.TileSuperMapRest({
  470. url: url,
  471. prjCoordSys: {"epsgCode": 4326}
  472. }));
  473. }
  474. }
  475. function resetSelect() {
  476. $('#unitSelect option:first').prop("selected", 'selected');
  477. $('#distance').val('');
  478. $('#bearing').val('');
  479. }
  480. //清除draw
  481. function clearInteraction() {
  482. if (draw) {
  483. map.removeInteraction(draw);
  484. }
  485. }
  486. //draw
  487. function drawing() {
  488. var buttons = $('.btn-group').children();
  489. buttons.map(function (key) {
  490. var value = buttons[key].value;
  491. if (value === 'None') {
  492. $(buttons[key]).on('click', function () {
  493. clearInteraction();
  494. });
  495. return;
  496. }
  497. if (value === 'Clear') {
  498. $(buttons[key]).on('click', function () {
  499. clearInteraction();
  500. drawSource.clear();
  501. });
  502. return;
  503. }
  504. $(buttons[key]).on('click', function () {
  505. clearInteraction();
  506. draw = new ol.interaction.Draw({
  507. source: drawSource,
  508. //type:('Point', 'LineString', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon' or 'Circle')
  509. type: buttons[key].value,
  510. snapTolerance: 20
  511. });
  512. map.addInteraction(draw);
  513. draw.on('drawend', function (evt) {
  514. coordinate = evt.feature.getGeometry().getCoordinates();
  515. clearInteraction();
  516. });
  517. });
  518. });
  519. drawSource = new ol.source.Vector({wrapX: false});
  520. var vector = new ol.layer.Vector({
  521. source: drawSource
  522. });
  523. map.addLayer(vector);
  524. }
  525. //右侧panel显示
  526. function showPanel() {
  527. $('.panel-body').show();
  528. }
  529. //右侧panel隐藏
  530. function hidePanel() {
  531. $('.panel-body').hide();
  532. $('.panel-body:eq(0)').show();
  533. }
  534. // 判断input是否是数字
  535. function checkIsNum(num) {
  536. var reg = /^\d+(\.\d+)?$/g;
  537. if (reg.test(num)) {
  538. return 1;
  539. } else {
  540. widgets.alert.showAlert(resources.msg_incorrectInfo, false);
  541. }
  542. }
  543. //判断是否在[-180,180]范围内
  544. function isInRange(val) {
  545. if (val < -180 || val > 180) {
  546. widgets.alert.showAlert(resources.msg_inputValue, false);
  547. }
  548. else {
  549. return 1;
  550. }
  551. }
  552. function greatThanZero(steps) {
  553. var reg = /^[1-9]\d*(\.\d+)?$/g;
  554. if (!reg.test(steps)) {
  555. widgets.alert.showAlert(resources.msg_stepSize, false);
  556. }
  557. }
  558. //设置center方法的icon
  559. function setCenterIcon() {
  560. turfSource.process("Helper.multiPoint", {
  561. //北京周边景区
  562. "coordinates": ([[116.38035099999959, 39.93393099410061], [116.34482800000002, 39.914760994108605], [116.40732099999964, 39.916013994108134], [116.44748600000042, 39.93767799409907]]),
  563. }, function (e) {
  564. geo = e;
  565. });
  566. }
  567. //设置distance方法的Icon
  568. function setDistanceIcon() {
  569. turfSource.process("Helper.multiPoint", {
  570. //北京2天津
  571. "coordinates": ([[116.407283, 39.90455699411283], [117.215268, 39.12096299445415]]),
  572. }, function (e) {
  573. geo = e;
  574. });
  575. }
  576. //设置destination方法的Icon
  577. function setDestinationIcon() {
  578. //设置起点
  579. var iconStyle1 = new ol.style.Style({
  580. image: new ol.style.Icon(({
  581. anchor: [0.5, 1],
  582. src: '../img/start_trans.png'
  583. }))
  584. });
  585. var start = new ol.geom.Point([116.407283, 39.90455699411283]);
  586. var feature1 = new ol.Feature(start);
  587. feature1.setStyle(iconStyle1);
  588. turfSource.addFeature(feature1);
  589. }
  590. //设置midpoint方法的Icon
  591. function setMidpointIcon() {
  592. turfSource.process("Helper.multiPoint", {
  593. //北京2天津
  594. "coordinates": ([[116.407283, 39.90455699411283], [117.215268, 39.12096299445415]]),
  595. }, function (e) {
  596. geo = e;
  597. });
  598. }
  599. //设置greatCircle方法的Icon
  600. function setGreatCircleIcon() {
  601. turfSource.process("Helper.multiPoint", {
  602. //北京2布拉格
  603. "coordinates": ([[116.407283, 39.90455699411283], [51.052494, 18.852219]])
  604. }, function (e) {
  605. geo = e;
  606. });
  607. }
  608. function setBezierLine() {
  609. if (coordinate) {
  610. turfSource.process("Helper.lineString", {
  611. "coordinates": coordinate,
  612. }, function (e) {
  613. geo = e;
  614. });
  615. }
  616. }
  617. //设置square的data
  618. function setSquarePolygon() {
  619. coordinate = [[[116.18865966796875, 39.78904724121094], [116.18865966796875, 40.008087158203125], [116.27998352050781, 40.008087158203125], [116.27998352050781, 39.78904724121094], [116.18865966796875, 39.78904724121094]]];
  620. turfSource.process("Helper.polygon", {
  621. "coordinates": (coordinate),
  622. }, function (e) {
  623. console.log(e);
  624. geo = e;
  625. });
  626. }
  627. //turf along方法
  628. function along() {
  629. var unit = $('#unitSelect').children('option:selected').val();
  630. if (unit != 'null' && unit != undefined) {
  631. widgets.alert.clearAlert();
  632. if (checkIsNum(distance) && coordinate) {
  633. turfSource.process("Helper.lineString", {
  634. "coordinates": coordinate,
  635. }, function (e) {
  636. resultSource.process("Measurement.along", {
  637. "line": e,
  638. "distance": distance,
  639. "units": unit,
  640. });
  641. }, false);
  642. }
  643. }
  644. else {
  645. widgets.alert.showAlert(resources.msg_selectUnit, false);
  646. }
  647. }
  648. //turf area方法
  649. function area() {
  650. if (coordinate) {
  651. turfSource.process("Helper.polygon", {
  652. "coordinates": (coordinate),
  653. }, function (polygon) {
  654. resultSource.process("Measurement.area", {
  655. "geojson": polygon
  656. }, function (e) {
  657. result = e + resources.msg_sqm;
  658. widgets.alert.showAlert(result, true);
  659. });
  660. });
  661. }
  662. }
  663. //turf bbox方法
  664. function bbox() {
  665. if (coordinate) {
  666. turfSource.process("Helper.lineString", {
  667. "coordinates": coordinate,
  668. }, function (e) {
  669. resultSource.process("Measurement.bbox", {
  670. "geojson": e
  671. }, function (bbox) {
  672. resultSource.process("Measurement.bboxPolygon", {
  673. "bbox": bbox
  674. });
  675. });
  676. });
  677. }
  678. }
  679. //turf destination方法
  680. function destination() {
  681. var unit = $('#unitSelect').children('option:selected').val();
  682. if (unit != 'null' && unit != undefined) {
  683. widgets.alert.clearAlert();
  684. if (checkIsNum(distance) && isInRange(bearing)) {
  685. turfSource.process("Helper.point", {
  686. //北京
  687. "coordinates": [116.407283, 39.90455699411283],
  688. }, function (point) {
  689. resultSource.process("Measurement.destination", {
  690. "origin": point,
  691. "distance": distance,
  692. "bearing": bearing,
  693. "units": unit,
  694. });
  695. }, false);
  696. }
  697. }
  698. else {
  699. widgets.alert.showAlert(resources.msg_selectUnit, false);
  700. }
  701. }
  702. //turf distance方法
  703. function turf_Distance() {
  704. var disUnits = {
  705. "kilometers": "千米",
  706. "miles": "英里",
  707. "degrees": "角度",
  708. "radians": "弧度",
  709. };
  710. var unit = $('#unitSelect').children('option:selected').val();
  711. if (unit != 'null' && unit != undefined) {
  712. widgets.alert.clearAlert();
  713. resultSource.process("Measurement.distance", {
  714. "from": geo.geometry.coordinates[0],
  715. "to": geo.geometry.coordinates[1],
  716. "units": unit,
  717. }, function (e) {
  718. resultNum = e;
  719. });
  720. unit = disUnits[unit];
  721. result = resultNum + unit;
  722. widgets.alert.showAlert(result, true);
  723. }
  724. else {
  725. widgets.alert.showAlert(resources.msg_selectUnit, false);
  726. }
  727. }
  728. //turf midpoint
  729. function midpoint() {
  730. if (geo) {
  731. resultSource.process("Measurement.midpoint", {
  732. "point1": geo.geometry.coordinates[0],
  733. "point2": geo.geometry.coordinates[1],
  734. });
  735. }
  736. }
  737. //turf greatCircle
  738. function greatCircle() {
  739. if (geo) {
  740. resultSource.process("Measurement.greatCircle", {
  741. "start": geo.geometry.coordinates[0],
  742. "end": geo.geometry.coordinates[1],
  743. "properties": {'name': '北京 to 布拉格'},
  744. "npoints": 100,
  745. "offset": 10
  746. });
  747. }
  748. }
  749. //turf square
  750. function square() {
  751. if (geo) {
  752. turfSource.process("Measurement.bbox", {
  753. "geojson": geo
  754. }, function (box) {
  755. resultSource.process("Measurement.square", {
  756. "bbox": box
  757. }, function (bbox) {
  758. resultSource.process("Measurement.bboxPolygon", {
  759. "bbox": bbox
  760. });
  761. }, null, false);
  762. }, null, false);
  763. }
  764. }
  765. //turf center
  766. function center() {
  767. if (geo) {
  768. resultSource.process("Measurement.center", {
  769. "geojson": geo
  770. });
  771. }
  772. }
  773. //turf bezierSpline
  774. function bezierSpline() {
  775. if (geo) {
  776. resultSource.process("Transformation.bezierSpline", {
  777. "line": geo,
  778. "resolution": 10000,
  779. "sharpness": 0.85
  780. });
  781. }
  782. }
  783. //turf buffer
  784. //先draw一个点 再在此点上进行缓冲区分析
  785. function buffer() {
  786. var unit = $('#unitSelect').children('option:selected').val();
  787. if (unit != 'null') {
  788. widgets.alert.clearAlert();
  789. greatThanZero(steps);
  790. if (coordinate && checkIsNum(distance) && checkIsNum(radius) && steps > 0) {
  791. turfSource.process("Helper.point", {
  792. "coordinates": coordinate,
  793. }, function (point) {
  794. resultSource.process("Transformation.buffer", {
  795. "geojson": point,
  796. "radius": radius,
  797. "steps": parseFloat(steps),
  798. "units": unit,
  799. });
  800. });
  801. } else {
  802. widgets.alert.showAlert(resources.msg_incorrectInfo, false);
  803. }
  804. }
  805. else {
  806. widgets.alert.showAlert(resources.msg_selectUnit, false);
  807. }
  808. }
  809. //turf circle
  810. //先draw一个点 再在此点上画圆多边形
  811. function circle() {
  812. var unit = $('#unitSelect').children('option:selected').val();
  813. widgets.alert.clearAlert();
  814. greatThanZero(steps);
  815. if (coordinate && checkIsNum(distance) && checkIsNum(radius) && steps > 0) {
  816. turfSource.process("Helper.point", {
  817. "coordinates": coordinate,
  818. }, function (point) {
  819. resultSource.process("Transformation.circle", {
  820. "center": point,
  821. "radius": radius,
  822. "steps": parseFloat(steps),
  823. "units": unit,
  824. "properties": {}
  825. });
  826. });
  827. }
  828. else {
  829. widgets.alert.showAlert(resources.msg_incorrectInfo, false);
  830. }
  831. }
  832. //清空图层
  833. function clearSource() {
  834. drawSource && drawSource.clear();
  835. turfSource && turfSource.clear();
  836. resultSource && resultSource.clear();
  837. }
  838. </script>
  839. </html>