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 point = info.geometry.coordinates;
  80. var element = __overlay.getElement();
  81. var descriptionHTML =
  82. "<div>code: " + info.properties.id + "</div>" +
  83. "<div>version: " + info.properties.version + "</div>" +
  84. "<div><a href='"+ site + dir + info.properties.path +"'>" + info.properties.path + "</a></div>" +
  85. "<div><a href='https://www.openstreetmap.org/#map=15/"+ point[1] + "/" + point[0] +"'>OpenStreetMap で現状を確認する</a></div>";
  86. element.innerHTML = descriptionHTML;
  87. __overlay.setPosition(coordinates);
  88. __map.addOverlay(__overlay);
  89. } else {
  90. __map.removeOverlay(__overlay);
  91. }
  92. });
  93.  
  94.  
  95. }
  96.  
  97.  
  98. // マップ表示のための中心位置を読み取る
  99. function loadMesh(dir) {
  100. $.when(
  101. $.getJSON("/task-bldg/city/"+ dir + geojson)
  102. ).done(function(data2) {
  103. features = data2.features;
  104. $(features).each(function() {
  105. if (this.properties != null) {
  106. if (this.properties.id == meshcode) {
  107. if (this.geometry != null) {
  108. if (this.geometry.type == "Point") {
  109. lonlat = this.geometry.coordinates; // 中心位置
  110. // マーカーの表示
  111. featurePoint = new ol.Feature({
  112. geometry: new ol.geom.Point(ol.proj.fromLonLat(lonlat))
  113. });
  114. featurePoint.information = this;
  115. featurePoint.setStyle(new ol.style.Style({
  116. image: new ol.style.Icon({
  117. src: './img/osm_200x200.png',
  118. anchor: [0.5, 0.5],
  119. scale: 0.2
  120. }),
  121. stroke: new ol.style.Stroke({color: '#ff33ff', width: 10})
  122. }));
  123. __markerLayer.getSource().addFeature(featurePoint);
  124. __map.getView().setCenter(ol.proj.fromLonLat(lonlat));
  125. }
  126. if (this.geometry.type == "LineString") {
  127. drawPolygon([this.geometry.coordinates]);
  128. }
  129. }
  130. }
  131. }
  132. });
  133. });
  134. }
  135.  
  136.  
  137. /**
  138. * ポリゴンを描画する
  139. ret: routeLayer
  140. */
  141. function drawPolygon(coordinates) {
  142. // ジオメトリの作成
  143. var polygon = new ol.geom.Polygon([]);
  144. polygon.setCoordinates(coordinates);
  145.  
  146. // 地物オブジェクトの作成 〜 レイヤーの作成
  147. var feature = new ol.Feature(
  148. polygon.transform('EPSG:4326', 'EPSG:3857')
  149. );
  150. feature.setId(meshcode);
  151.  
  152. var vector = new ol.source.Vector({
  153. features: [feature]
  154. });
  155. routeLayer = new ol.layer.Vector({
  156. source: vector,
  157. style: new ol.style.Style({
  158. stroke: new ol.style.Stroke({ color: '#000000', width: 2 })
  159. //fill: new ol.style.Fill({ color: [0, 0, 0, 0.2] })
  160. })
  161. });
  162.  
  163. // 作成したポリゴンをレイヤーにのせる
  164. __map.addLayer(routeLayer);
  165. }
  166.