osmbuildings.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_osmBuildings"></title>
  9. <script type="text/javascript" include="jquery" src="../js/include-web.js"></script>
  10. <!-- 此范例基于 openlayers@4.6.5 -->
  11. <script type="text/javascript" include="ol@4.6.5,osmbuildings" src="../../dist/ol/include-ol.js"></script>
  12. <style>
  13. .ol-popup {
  14. position: relative;
  15. background-color: white;
  16. -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  17. filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  18. padding: 15px;
  19. border-radius: 10px;
  20. border: 1px solid #cccccc;
  21. bottom: 60px;
  22. left: -50px;
  23. font-size: 14px;
  24. z-index: 100;
  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. .ol-popup-closer {
  48. text-decoration: none;
  49. position: absolute;
  50. top: 2px;
  51. right: 8px;
  52. }
  53. .ol-popup-closer:after {
  54. content: "✖";
  55. }
  56. </style>
  57. </head>
  58. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  59. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  60. <div id="popup" class="ol-popup">
  61. <a href="#" id="popup-closer" class="ol-popup-closer"></a>
  62. <div id="popup-content"></div>
  63. </div>
  64. <script>
  65. var container = document.getElementById('popup');
  66. var content = document.getElementById('popup-content');
  67. var closer = document.getElementById('popup-closer');
  68. var overlay = new ol.Overlay(({
  69. element: container,
  70. autoPan: true,
  71. autoPanAnimation: {
  72. duration: 250
  73. }
  74. }));
  75. var map = new ol.Map({
  76. target: 'map',
  77. view: new ol.View({
  78. center: ol.proj.transform([116.450, 39.916], 'EPSG:4326', 'EPSG:3857'),
  79. zoom: 16,
  80. }),
  81. overlays: [overlay]
  82. });
  83. var url = (window.isLocal ? window.server : "https://iserver.supermap.io") + "/iserver/services/map-china400/rest/maps/China";
  84. var layer = new ol.layer.Tile({
  85. source: new ol.source.TileSuperMapRest({
  86. url: url,
  87. wrapX: true
  88. }),
  89. projection: 'EPSG:3857'
  90. });
  91. map.addLayer(layer);
  92. loadData();
  93. function loadData() {
  94. var data;
  95. $.get('../data/buildings.json', function (geojson) {
  96. data = geojson;
  97. new OSMBuildings(map)
  98. .date(new Date(2017, 5, 15, 17, 30))
  99. .set(geojson)
  100. .click(bindPopup);
  101. });
  102. function bindPopup(evt) {
  103. var name = getFeatureNameById(evt.feature);
  104. name = name || "<span style='color:red'>" + resources.text_noData + "</span>";
  105. content.innerHTML = name;
  106. overlay.setPosition([evt.lat, evt.lon]);
  107. closer.onclick = function () {
  108. overlay.setPosition(undefined);
  109. closer.blur();
  110. return false;
  111. };
  112. }
  113. function getFeatureNameById(id) {
  114. if (!data || !id) {
  115. return null;
  116. }
  117. var features = data.features;
  118. for (var i = 0; i < features.length; i++) {
  119. if (features[i].properties.id === id) {
  120. return features[i].properties.name;
  121. }
  122. }
  123. }
  124. }
  125. </script>
  126. </body>
  127. </html>