Newer
Older
task-bldg / src / main / resources / static / custom / tasks.js
  1. const geojson = "index.geojson";
  2.  
  3. var __map = null;
  4. var __markerLayer = null;
  5. var __lineLayer = null;
  6.  
  7. var dir = "./";
  8. var cityname = "";
  9. var line = [];
  10.  
  11. var queryString = window.location.search;
  12. var queryObject = new Object();
  13. if(queryString){
  14. queryString = queryString.substring(1);
  15. var parameters = queryString.split('&');
  16. for (var i = 0; i < parameters.length; i++) {
  17. var element = parameters[i].split('=');
  18. var paramName = decodeURIComponent(element[0]);
  19. var paramValue = decodeURIComponent(element[1]);
  20. queryObject[paramName] = paramValue;
  21. }
  22. }
  23. const mesh = /*[[${mesh}]]*/"mesh";
  24. const citycode = queryObject['citycode'];
  25. const meshcode = queryObject['meshcode'];
  26.  
  27. function loadMap() {
  28. // 'citycode'に対応するデータを読む
  29. var lonlat = [139.7637,35.6808];
  30. $.when(
  31. $.getJSON("/task-bldg/city/index.json")
  32. ).done(function(data1) {
  33. site = data1.site;
  34. $(data1.list).each(function() {
  35. if (this.code == citycode) {
  36. dir = this.path + "/bldg/";
  37. cityname = this.name;
  38. loadMesh(dir, citycode, meshcode);
  39. }
  40. })
  41. });
  42.  
  43.  
  44. // マップの作成
  45. __map = new ol.Map({
  46. target: 'map',
  47. layers: [
  48. new ol.layer.Tile({
  49. source: new ol.source.OSM()
  50. })
  51. ],
  52. view: new ol.View({
  53. center: ol.proj.fromLonLat(lonlat),
  54. zoom: 15
  55. })
  56. });
  57. // 表示するためのレイヤーを作成する
  58. __markerLayer = new ol.layer.Vector({
  59. source: new ol.source.Vector()
  60. });
  61. __map.addLayer(__markerLayer);
  62. // ポップアップを表示するためのオーバーレイを作成する
  63. __overlay = new ol.Overlay({
  64. element: document.getElementById('popup'),
  65. positioning: 'bottom-center'
  66. });
  67.  
  68. // 地図のクリックイベントを設定
  69. __map.on('click', function (evt) {
  70. var feature = __map.forEachFeatureAtPixel(
  71. evt.pixel,
  72. function (feature) {
  73. return feature;
  74. }
  75. );
  76. if (feature) {
  77. var coordinates = feature.getGeometry().getCoordinates();
  78. var info = feature.information;
  79. var element = __overlay.getElement();
  80. var descriptionHTML =
  81. "<div>code: " + info.properties.id + "</div>" +
  82. "<div>version: " + info.properties.version + "</div>" +
  83. "<div><a href='/task-bldg"+ site + dir + info.properties.path +"'>" + info.properties.path + "</a></div>";
  84. element.innerHTML = descriptionHTML;
  85. __overlay.setPosition(coordinates);
  86. __map.addOverlay(__overlay);
  87. } else {
  88. __map.removeOverlay(__overlay);
  89. }
  90. });
  91.  
  92.  
  93. }
  94.  
  95.  
  96. // マップ表示のための中心位置を読み取る
  97. function loadMesh(dir) {
  98. $.when(
  99. $.getJSON("/task-bldg/city/"+ dir + geojson)
  100. ).done(function(data2) {
  101. features = data2.features;
  102. $(features).each(function() {
  103. if (this.properties != null) {
  104. if (this.properties.id == meshcode) {
  105. if (this.geometry != null) {
  106. if (this.geometry.type == "Point") {
  107. lonlat = this.geometry.coordinates; // 中心位置
  108. // マーカーの表示
  109. featurePoint = new ol.Feature({
  110. geometry: new ol.geom.Point(ol.proj.fromLonLat(lonlat))
  111. });
  112. featurePoint.information = this;
  113. featurePoint.setStyle(new ol.style.Style({
  114. image: new ol.style.Icon({
  115. src: './img/osm_200x200.png',
  116. anchor: [0.5, 0.5],
  117. scale: 0.2
  118. }),
  119. stroke: new ol.style.Stroke({color: '#ff33ff', width: 10})
  120. }));
  121. __markerLayer.getSource().addFeature(featurePoint);
  122. __map.getView().setCenter(ol.proj.fromLonLat(lonlat));
  123. }
  124. if (this.geometry.type == "LineString") {
  125. drawPolygon([this.geometry.coordinates]);
  126. }
  127. }
  128. }
  129. }
  130. });
  131. });
  132. }
  133.  
  134.  
  135. /**
  136. * ポリゴンを描画する
  137. ret: routeLayer
  138. */
  139. function drawPolygon(coordinates) {
  140. // ジオメトリの作成
  141. var polygon = new ol.geom.Polygon([]);
  142. polygon.setCoordinates(coordinates);
  143.  
  144. // 地物オブジェクトの作成 〜 レイヤーの作成
  145. var feature = new ol.Feature(
  146. polygon.transform('EPSG:4326', 'EPSG:3857')
  147. );
  148. feature.setId(meshcode);
  149.  
  150. var vector = new ol.source.Vector({
  151. features: [feature]
  152. });
  153. routeLayer = new ol.layer.Vector({
  154. source: vector,
  155. style: new ol.style.Style({
  156. stroke: new ol.style.Stroke({ color: '#000000', width: 2 })
  157. //fill: new ol.style.Fill({ color: [0, 0, 0, 0.2] })
  158. })
  159. });
  160.  
  161. // 作成したポリゴンをレイヤーにのせる
  162. __map.addLayer(routeLayer);
  163. }
  164.