Browse Source

地图显示

laiguoran 4 years ago
parent
commit
7ec403fe96
2 changed files with 57 additions and 22 deletions
  1. 49 13
      app/view/tender/detail.ejs
  2. 8 9
      app/view/tender/detail_modal.ejs

+ 49 - 13
app/view/tender/detail.ejs

@@ -1152,23 +1152,59 @@
         };
     })
 </script>
+<% if (hadMap || ctx.session.sessionUser.is_admin) { %>
+<script src="/public/js/map/turf.min.js"></script>
+<script>
+    const tenderMapList = JSON.parse(unescape('<%- escape(JSON.stringify(tenderMapList)) %>'));
+</script>
+<% } %>
 <% if (hadMap) { %>
 <!--<script src="//bj.bcebos.com/v1/mapopen/api-demos/js/mapStyle.js"></script>-->
 <script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=C3hLZAjuWTaCdwnwoYY83APrwlPEj4v7"></script>
 <script type="text/javascript">
-    var map = new BMapGL.Map("map");
-    // 创建地图实例
-    var point = new BMapGL.Point(113.462, 22.2245);
-    // 初始化地图,设置中心点坐标和地图级别
-    map.centerAndZoom(point, 12);
-    map.enableScrollWheelZoom(true);
-    map.setMapStyleV2({styleId: '20d4aea41cf71387395f2dc835f1c4b6'});
     // 画线
-    var polyline = new BMapGL.Polyline([
-        new BMapGL.Point(113.352, 22.166),
-        new BMapGL.Point(113.572, 22.283),
-        // new BMapGL.Point(116.425, 39.900)
-    ], {strokeColor:"red", strokeWeight:5, strokeOpacity:1});
-    map.addOverlay(polyline);
+    $(function () {
+        const map = new BMapGL.Map("map");
+        // 创建地图实例
+
+        const polyLineList = [];
+        const centerPoint = { lng: 116.404, lat: 39.915 };
+        const pointList = [];
+        for (const tm of tenderMapList) {
+            if (tm.map_json) {
+                const map_json = JSON.parse(unescape(escape(tm.map_json)));
+                const lngLatList = [];
+                for (const mj of map_json) {
+                    lngLatList.push(new BMapGL.Point(mj.lng, mj.lat));
+                }
+                polyLineList.push({
+                    map: lngLatList,
+                    color: tm.color,
+                });
+                const center = JSON.parse(tm.center);
+                pointList.push(turf.point([center.lng, center.lat]));
+            }
+        }
+        if (pointList.length > 0) {
+            const features = turf.featureCollection(pointList);
+            const center = turf.center(features);
+            if (center && center.geometry && center.geometry.coordinates && center.geometry.coordinates.length === 2) {
+                centerPoint.lng = _.round(center.geometry.coordinates[0], 4);
+                centerPoint.lat = _.round(center.geometry.coordinates[1], 4);
+            }
+        }
+        // 初始化地图,设置中心点坐标和地图级别
+        const point = new BMapGL.Point(centerPoint.lng, centerPoint.lat);
+        map.centerAndZoom(point, 14);
+        map.enableScrollWheelZoom(true);
+        map.setMapStyleV2({styleId: '20d4aea41cf71387395f2dc835f1c4b6'});
+        if (polyLineList.length > 0) {
+            for (const pl of polyLineList) {
+                const polyline = new BMapGL.Polyline(pl.map, {strokeColor: pl.color, strokeWeight:5, strokeOpacity:1});
+                map.addOverlay(polyline);
+            }
+        }
+    })
+
 </script>
 <% } %>

+ 8 - 9
app/view/tender/detail_modal.ejs

@@ -2025,7 +2025,9 @@
             </div>
             <div class="modal-footer">
                 <button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">关闭</button>
-                <button type="button" class="btn btn-sm btn-success" >刷新显示</button>
+                <% if (hadMap) { %>
+                <button type="button" class="btn btn-sm btn-success" onclick="window.location.reload()" >刷新显示</button>
+                <% } %>
             </div>
         </div>
     </div>
@@ -2085,10 +2087,9 @@
         </div>
     </div>
 </div>
-<script src="/public/js/map/turf.min.js"></script>
+
 <script>
     const hadMap = parseInt(<%- hadMap %>);
-    const tenderMapList = JSON.parse(unescape('<%- escape(JSON.stringify(tenderMapList)) %>'));
     $(function () {
         $('body').on('click', '#bd-set-11 .select-map', function () {
             // const val = parseInt($(this).data('value'));
@@ -2157,8 +2158,6 @@
             $('.save-map').attr('data-mid', id);
             // 加载sjs
             const mapInfo = _.find(tenderMapList, { id });
-            console.log(mapInfo);
-            // mapInfo.map_json = [{ tip: 'K100~101', lng: 123.45, lat: 23.45}];
             $('#map-name').val(mapInfo.name);
             $('#map-color').val(mapInfo.color);
             SpreadJsObj.loadSheetData(mapSpread.getActiveSheet(), SpreadJsObj.DataType.Data, (mapInfo.map_json ? JSON.parse(unescape(escape(mapInfo.map_json))) : []));
@@ -2181,8 +2180,8 @@
                     const curRow = info.row;
                     info.sheet.zh_data[curRow] = {
                         tip: info.sheet.getText(curRow, 0),
-                        x: info.sheet.getText(curRow, 1) !== '' ? _.toNumber(info.sheet.getText(curRow, 1)) : '',
-                        y: info.sheet.getText(curRow, 2) !== '' ? _.toNumber(info.sheet.getText(curRow, 2)) : '',
+                        lng: info.sheet.getText(curRow, 1) !== '' ? _.toNumber(info.sheet.getText(curRow, 1)) : '',
+                        lat: info.sheet.getText(curRow, 2) !== '' ? _.toNumber(info.sheet.getText(curRow, 2)) : '',
                     }
                 }
             },
@@ -2212,8 +2211,8 @@
                     const curRow = range.row + iRow;
                     info.sheet.zh_data[curRow] = {
                         tip: info.sheet.getText(curRow, 0),
-                        x: info.sheet.getText(curRow, 1) !== '' ? _.toNumber(info.sheet.getText(curRow, 1)) : '',
-                        y: info.sheet.getText(curRow, 2) !== '' ? _.toNumber(info.sheet.getText(curRow, 2)) : '',
+                        lng: info.sheet.getText(curRow, 1) !== '' ? _.toNumber(info.sheet.getText(curRow, 1)) : '',
+                        lat: info.sheet.getText(curRow, 2) !== '' ? _.toNumber(info.sheet.getText(curRow, 2)) : '',
                     }
                 }
             },