graphicLayer.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. <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_mb_graphicLayer"></title>
  11. <style>
  12. body {
  13. margin: 0;
  14. overflow: hidden;
  15. background: #fff;
  16. width: 100%;
  17. height: 100%
  18. }
  19. #map {
  20. position: absolute;
  21. top: 0;
  22. bottom: 0;
  23. width: 100%;
  24. }
  25. #title {
  26. position: absolute;
  27. color: white;
  28. left: 0;
  29. top: 30px;
  30. text-align: center;
  31. width: 100%;
  32. z-index: 2;
  33. }
  34. #title > h3{
  35. margin: 10px 0;
  36. letter-spacing: 0.1em;
  37. }
  38. #title > h6 {
  39. margin: 0;
  40. font-weight: normal;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div id="title">
  46. <h3 data-i18n="resources.text_graphicLayer_title"></h3>
  47. <h6 data-i18n="resources.text_graphicLayer_subTitle"></h6>
  48. </div>
  49. <div id="map"></div>
  50. <script type="text/javascript" include="papaparse,jquery,dat-gui,widgets" src="../js/include-web.js"></script>
  51. <script type="text/javascript" include="deck" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
  52. <script type="text/javascript">
  53. var host = window.isLocal ? window.server : "https://iserver.supermap.io",
  54. url = host + "/iserver/services/map-china400/rest/maps/ChinaDark";
  55. var map, graphicLayer;
  56. var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
  57. " with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
  58. " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
  59. map = new mapboxgl.Map({
  60. container: 'map',
  61. style: {
  62. "version": 8,
  63. "sources": {
  64. "raster-tiles": {
  65. "attribution": attribution,
  66. "type": "raster",
  67. "tiles": [url + '/zxyTileImage.png?z={z}&x={x}&y={y}'],
  68. "tileSize": 256,
  69. },
  70. },
  71. "layers": [{
  72. "id": "simple-tiles",
  73. "type": "raster",
  74. "source": "raster-tiles",
  75. "minzoom": 0,
  76. "maxzoom": 22
  77. }]
  78. },
  79. center: [-73.91426, 40.7594],
  80. zoom: 10.64
  81. });
  82. map.addControl(new mapboxgl.NavigationControl(), 'top-left');
  83. widgets.loader.showLoader("data loading...");
  84. $.get('../data/nyc-taxi.csv', function (csvstr) {
  85. widgets.loader.removeLoader();
  86. var result = Papa.parse(csvstr, {skipEmptyLines: true, header: true});
  87. addLayer(result.data);
  88. });
  89. function addLayer(points) {
  90. var graphics = [], popup = new mapboxgl.Popup({closeOnClick: false}).addTo(map);
  91. for (var i = 0; i < points.length; i++) {
  92. var lngLat = {
  93. lng: parseFloat(points[i].lng),
  94. lat: parseFloat(points[i].lat)
  95. };
  96. /**
  97. * 可以单独给要素设置颜色和半径:
  98. * new mapboxgl.supermap.Graphic(lngLat,{
  99. * color:[255,0,0],
  100. * radius:40
  101. * });
  102. */
  103. graphics.push(new mapboxgl.supermap.Graphic(lngLat));
  104. }
  105. var graphicStyle = {
  106. color: [0, 255, 128],
  107. radius: 10
  108. };
  109. graphicLayer = new mapboxgl.supermap.GraphicLayer("graphic", {
  110. graphics: graphics,
  111. radius: graphicStyle.radius,
  112. color: graphicStyle.color,
  113. highlightColor: [255, 0, 0, 255],
  114. onClick: function (e) {
  115. if (!popup.isOpen()) {
  116. popup.addTo(map);
  117. }
  118. popup.setLngLat(e.lngLat)
  119. .setHTML("position:" + JSON.stringify(e.lngLat))
  120. }
  121. });
  122. map.addLayer(graphicLayer);
  123. initDatGui(graphicStyle)
  124. }
  125. //设置面板
  126. function initDatGui(options) {
  127. var gui = new dat.GUI();
  128. var Control = createConfigControl();
  129. map.addControl(new Control(gui.domElement), 'top-right');
  130. gui.addColor(options, 'color').onChange(finished);
  131. gui.add(options, 'radius', 0, 100).onChange(finished);
  132. function finished() {
  133. graphicLayer.setStyle(options);
  134. }
  135. }
  136. //创建图层操作控件
  137. function createConfigControl() {
  138. function ConfigControl(domElement) {
  139. this.dom = domElement;
  140. }
  141. ConfigControl.prototype.onAdd = function (map) {
  142. this._map = map;
  143. this._container = this.dom;
  144. this._container.className = 'mapboxgl-ctrl ' + this._container.className;
  145. return this._container;
  146. };
  147. ConfigControl.prototype.onRemove = function () {
  148. this._container.parentNode.removeChild(this._container);
  149. this._map = undefined;
  150. };
  151. return ConfigControl;
  152. }
  153. </script>
  154. </body>
  155. </html>