07_graphiclayer_webgl.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html lang="en-US">
  6. <head>
  7. <meta charset="UTF-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  9. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  10. <title data-i18n="resources.title_graphiclayerWebgl"></title>
  11. <script type="text/javascript" include="randomcolor,papaparse,widgets" src="../js/include-web.js"></script>
  12. <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
  13. <style>
  14. .ol-popup {
  15. position: absolute;
  16. background-color: white;
  17. -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  18. filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  19. padding: 15px;
  20. border-radius: 10px;
  21. border: 1px solid #cccccc;
  22. bottom: 12px;
  23. left: -50px;
  24. min-width: 50px;
  25. }
  26. .ol-popup:after, .ol-popup:before {
  27. top: 100%;
  28. border: solid transparent;
  29. content: " ";
  30. height: 0;
  31. width: 0;
  32. position: absolute;
  33. pointer-events: none;
  34. }
  35. .ol-popup:after {
  36. border-top-color: white;
  37. border-width: 10px;
  38. left: 48px;
  39. margin-left: -10px;
  40. }
  41. .ol-popup:before {
  42. border-top-color: #cccccc;
  43. border-width: 11px;
  44. left: 48px;
  45. margin-left: -11px;
  46. }
  47. </style>
  48. </head>
  49. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
  50. <div id="map" style="width: 100%;height:100%"></div>
  51. <div id="popup" class="ol-popup">
  52. <div id="popup-content"></div>
  53. </div>
  54. <script type="text/javascript">
  55. var url = (window.isLocal ? window.server : "https://iserver.supermap.io") + "/iserver/services/map-china400/rest/maps/ChinaDark",
  56. container = document.getElementById('popup'),
  57. content = document.getElementById('popup-content'),
  58. overlay = new ol.Overlay(({
  59. element: container,
  60. autoPan: true,
  61. autoPanAnimation: {
  62. duration: 250
  63. }
  64. })),
  65. map = new ol.Map({
  66. target: 'map',
  67. controls: ol.control.defaults({attributionOptions: {collapsed: false}})
  68. .extend([new ol.supermap.control.Logo()]),
  69. view: new ol.View({
  70. center: ol.proj.transform([-73.9286, 40.75], 'EPSG:4326', 'EPSG:3857'),
  71. zoom: 12,
  72. projection: 'EPSG:3857',
  73. multiWorld: true
  74. }),
  75. overlays: [overlay]
  76. });
  77. var colorCount = 10;
  78. var colors = getRandomColors(colorCount);
  79. loadData();
  80. function loadData() {
  81. widgets.loader.showLoader();
  82. $.get('../data/nyc_taxi_18W.csv', function (nycData) {
  83. new ol.supermap.MapService(url).getMapInfo(function (serviceResult) {
  84. var mapJSONObj = serviceResult.result;
  85. var options = ol.source.TileSuperMapRest.optionsFromMapJSON(url, mapJSONObj);
  86. var layer = new ol.layer.Tile({
  87. source: new ol.source.TileSuperMapRest(options)
  88. });
  89. map.addLayer(layer);
  90. var randomCircleStyles = [];
  91. for (var i = 0; i < colorCount; i++) {
  92. randomCircleStyles.push(new ol.style.Circle({
  93. radius: Math.floor(Math.random() * 3 + 1),
  94. fill: new ol.style.Fill({
  95. color: colors[i]
  96. }),
  97. stroke: new ol.style.Stroke({
  98. color: colors[i]
  99. }),
  100. }));
  101. }
  102. var features = Papa.parse(nycData, {skipEmptyLines: true, header: true}).data;
  103. var count = features.length; //矢量点的个数
  104. var graphics = new Array(count);
  105. for (var i = 0; i < count; ++i) {
  106. var coordinates = [Number(features[i].X), Number(features[i].Y)];
  107. if (coordinates[0] === coordinates[1]) {
  108. continue;
  109. }
  110. coordinates = ol.proj.transform(coordinates, 'EPSG:4326', 'EPSG:3857');
  111. graphics[i] = new ol.Graphic(new ol.geom.Point(coordinates));
  112. graphics[i].setStyle(randomCircleStyles[Math.floor(Math.random() * colorCount)]);
  113. }
  114. map.once('postrender', function () {
  115. var graphicLayer = new ol.layer.Image({
  116. source: new ol.source.Graphic({
  117. graphics: graphics,
  118. render: "canvas",
  119. map: map,
  120. onClick: function (graphic) {
  121. if (graphic) {
  122. var coords = graphic.getGeometry().getCoordinates();
  123. content.innerHTML = resources.text_coordinate + ":[" + coords[0] + "," + coords[1] + "]";
  124. overlay.setPosition(graphic.getGeometry().getCoordinates());
  125. return;
  126. }
  127. overlay.setPosition(undefined);
  128. }
  129. })
  130. });
  131. map.addLayer(graphicLayer);
  132. widgets.loader.removeLoader();
  133. })
  134. });
  135. });
  136. }
  137. function getRandomColors(count) {
  138. return randomColor({
  139. luminosity: 'bright',
  140. hue: 'random',
  141. alpha: 0.5,
  142. format: 'rgba',
  143. count: count
  144. });
  145. }
  146. </script>
  147. </body>
  148. </html>