snapDrawFeatures.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="UTF-8">
  8. <title data-i18n="resources.title_snapDrawFeatures"></title>
  9. <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
  10. <style>
  11. .ol-popup {
  12. position: absolute;
  13. top: 50px;
  14. right: 20px;
  15. }
  16. </style>
  17. </head>
  18. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  19. <div id="map" style="width: 100%;height:100%"></div>
  20. <div id="popup" class="ol-popup">
  21. <div class="btn-group" role="group" aria-label="...">
  22. <button id="drawPoint" value='Point' type="button" class="btn btn-default"
  23. data-i18n="resources.text_input_value_drawPoint">
  24. </button>
  25. <button id="drawLine" value='LineString' type="button" class="btn btn-default"
  26. data-i18n="resources.text_input_value_drawLine">
  27. </button>
  28. <button id="drawPolygon" value='Polygon' type="button" class="btn btn-default"
  29. data-i18n="resources.text_input_value_drawPolygon">
  30. </button>
  31. <button id="drawCircle" value='Circle' type="button" class="btn btn-default"
  32. data-i18n="resources.btn_drawCircle">
  33. </button>
  34. <button id="none" value='None' type="button" class="btn btn-default" data-i18n="resources.btn_notDraw">
  35. </button>
  36. <button id="clear" value='Clear' type="button" class="btn btn-default"
  37. data-i18n="resources.text_input_value_clear">
  38. </button>
  39. </div>
  40. </div>
  41. <script type="text/javascript" include="bootstrap,jquery" src="../js/include-web.js"></script>
  42. <script type="text/javascript">
  43. var map, draw, snap,
  44. url = (window.isLocal ? window.server : "https://iserver.supermap.io") + "/iserver/services/map-china400/rest/maps/China";
  45. map = new ol.Map({
  46. target: 'map',
  47. controls: ol.control.defaults({attributionOptions: {collapsed: false}})
  48. .extend([new ol.supermap.control.Logo()]),
  49. view: new ol.View({
  50. center: [12957388, 4853991],
  51. zoom: 4,
  52. projection: 'EPSG:3857',
  53. multiWorld: true
  54. })
  55. });
  56. var layer = new ol.layer.Tile({
  57. source: new ol.source.TileSuperMapRest({
  58. url: url,
  59. wrapX: true
  60. }),
  61. projection: 'EPSG:3857'
  62. });
  63. var source = new ol.source.Vector({wrapX: false});
  64. var vector = new ol.layer.Vector({
  65. source: source
  66. });
  67. map.addLayer(layer);
  68. map.addLayer(vector);
  69. var info = new ol.control.Control({element: document.getElementById('popup')});
  70. info.setMap(map);
  71. map.addControl(info);
  72. var buttons = $('.btn-group').children();
  73. buttons.map(function (key) {
  74. var value = buttons[key].value;
  75. if (value === 'None') {
  76. $(buttons[key]).on('click', function () {
  77. clearInteraction();
  78. });
  79. return;
  80. }
  81. if (value === 'Clear') {
  82. $(buttons[key]).on('click', function () {
  83. clearInteraction();
  84. source.clear();
  85. });
  86. return;
  87. }
  88. $(buttons[key]).on('click', function () {
  89. clearInteraction();
  90. draw = new ol.interaction.Draw({
  91. source: source,
  92. type: buttons[key].value,
  93. snapTolerance: 20
  94. });
  95. map.addInteraction(draw);
  96. snap = new ol.interaction.Snap({
  97. source: source
  98. });
  99. map.addInteraction(snap);
  100. });
  101. });
  102. function clearInteraction() {
  103. if (draw) {
  104. map.removeInteraction(draw);
  105. }
  106. if (snap) {
  107. map.removeInteraction(snap);
  108. }
  109. }
  110. </script>
  111. </body>
  112. </html>