From 3df944d30530be8dc0ea1cbe1ed4afc22eb160a5 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期四, 06 二月 2025 11:09:48 +0800
Subject: [PATCH] 1.添加数据更新功能,确保本地没有数据时再获取基础数据 2.地图界面上添加滚动功能的控件(部分功能)

---
 app/src/main/assets/js/map.js |  375 +++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 304 insertions(+), 71 deletions(-)

diff --git a/app/src/main/assets/js/map.js b/app/src/main/assets/js/map.js
index 8309b68..85b3113 100644
--- a/app/src/main/assets/js/map.js
+++ b/app/src/main/assets/js/map.js
@@ -1,15 +1,31 @@
 (function () {
-    // 鍥剧墖璺緞
-    const locationIMGPath = 'img/location.png';
-    const markerBalckPath = 'img/marker_black.svg';
-    const markerRedPath = 'img/marker_red.svg';
-    const markerBluePath = 'img/marker_blue.svg';
-    const centerPin = 'img/push_pin.svg';
+    // 寤鸿灏嗚繖浜涘父閲忕Щ鍒颁竴涓厤缃璞′腑缁熶竴绠$悊
+    const CONFIG = {
+        IMAGES: {
+            LOCATION: 'img/location.png',
+            MARKER_BLACK: 'img/marker_black.svg',
+            MARKER_RED: 'img/marker_red.svg',
+            MARKER_BLUE: 'img/marker_blue.svg',
+            CENTER_PIN: 'img/push_pin.svg',
+            DIVIDE_BLUE: 'img/divide_home_blue.svg',
+            DIVIDE_RED: 'img/divide_home_red.svg'
+        },
+        MAP: {
+            DEFAULT_ZOOM: 12,
+            TIANDITU_KEY: 'd8beed89b43160a9a185e5aff431f85d'
+        }
+    };
     let map;
     let lastMarker = null;
     let lastClickedMarker = null;
+    let lastClickedDivide = null;
     let isShowWaterIntakeDetail = false;
+    let isShowDivideDetail = false;
     let zoom = 12;
+
+    // 瀛樺偍鎵�鏈夌缃戠嚎璺殑鏁扮粍
+    let pipeLineList = [];
+    let currentPipePath = [];
 
     // 灏嗘柟娉曟寕杞藉埌 window 涓�
     function mountMethodToWindow() {
@@ -25,7 +41,11 @@
         window.showPin = showPin;
         window.cancelPin = cancelPin;
         window.refreshMarker = refreshMarker;
-
+        window.addDivide = addDivide;
+        window.showAllPipeLines = showAllPipeLines;
+        window.hideAllPipeLines = hideAllPipeLines;
+        window.clearAllPipeLines = clearAllPipeLines;
+        window.addPipeNetwork = addPipeNetwork;
     }
 
 
@@ -115,13 +135,15 @@
     function locationOverLay(lng, lag) {
         console.log("function銆嬨�嬨�嬨�嬨�媗ocationOverLay");
         map.centerAndZoom(new T.LngLat(lng, lag), map.getZoom());
-
         let icon = new T.Icon({
-            iconUrl: locationIMGPath,
-            iconSize: new T.Point(27, 27),
-            iconAnchor: new T.Point(10, 25)
+            iconUrl: CONFIG.IMAGES.LOCATION,
+            iconSize: new T.Point(20, 20),
+            iconAnchor: new T.Point(10, 10)
         });
-        let marker = new T.Marker(new T.LngLat(lng, lag), { icon: icon });
+        let marker = new T.Marker(
+            new T.LngLat(lng, lag),
+            { icon: icon }
+        );
         map.addOverLay(marker);
     }
 
@@ -146,16 +168,13 @@
     function closeWaterIntakeDetail(data) {
         if (!isShowCenterPin) {
             if (lastClickedMarker !== null) {
-                // 濡傛灉鏈夌偣鍑诲彇姘村彛鍒欏皢鍙栨按鍙f仮澶嶆垚榛戣壊
-                // 涓婃鐐瑰嚮鐨勬爣娉ㄦ敼涓洪粦鑹�
-                var defaulticon = new T.Icon({
-                    iconUrl: markerBluePath,
-                    iconSize: new T.Point(28, 28),
-                    iconAnchor: new T.Point(14, 28)
-                });
-                lastClickedMarker.setIcon(defaulticon);
+                lastClickedMarker.setIcon(createIcon(CONFIG.IMAGES.MARKER_BLUE));
             }
-            if (isShowWaterIntakeDetail) {
+            if(lastClickedDivide!==null)
+           {
+           lastClickedDivide.setIcon(createIcon(CONFIG.IMAGES.DIVIDE_BLUE));
+           }
+            if (isShowWaterIntakeDetail || isShowDivideDetail) {
                 // 鍋囧鏄剧ず浜嗗彇姘村彛璇︽儏鍒欓殣钘忓彇姘村彛璇︽儏
                 isShowWaterIntakeDetail = false;
                 window.Android.closeWaterIntakeView();
@@ -164,7 +183,6 @@
                 getLngLat(data.lnglat);
             }
         }
-
     }
 
     // 鐐瑰嚮鏍囨敞鐨勪簨浠�
@@ -172,6 +190,8 @@
         if (!isShowCenterPin) {
             chageMarkerIcon(data);
             showWaterIntakeDetail(id);
+        } else {
+            showToast("褰撳墠姝e湪淇敼閫変腑鍙栨按鍙g殑缁忕含搴︼紝瀹屾垚鎴栭��鍑哄悗鎵嶅彲閫夋嫨鍏朵粬锛�");
         }
 
     }
@@ -180,32 +200,23 @@
     function chageMarkerIcon(data) {
         // 鐐瑰嚮鐨勬爣娉ㄦ敼涓虹孩鑹�
         var currentMarker = data.target;
-        // 鐐瑰嚮鍚庡浘鏍�
-        var clickedIcon = new T.Icon({
-            iconUrl: markerRedPath,
-            iconSize: new T.Point(28, 28),
-            iconAnchor: new T.Point(14, 28)
-        });
-        // 璁剧疆褰撳墠鐐瑰嚮鐨� marker 涓虹偣鍑诲悗鍥炬爣
-        currentMarker.setIcon(clickedIcon);
-        // 鍒ゆ柇鐐瑰嚮鐨勪笉鏄悓涓�涓潗鏍�
+        currentMarker.setIcon(createIcon(CONFIG.IMAGES.MARKER_RED));
+
         if (lastClickedMarker !== null) {
             if (!isEqualsLngLat(data.target.getLngLat(), lastClickedMarker.getLngLat())) {
-                var defaulticon = new T.Icon({
-                    iconUrl: markerBluePath,
-                    iconSize: new T.Point(28, 28),
-                    iconAnchor: new T.Point(14, 28)
-                });
-                lastClickedMarker.setIcon(defaulticon);
+                lastClickedMarker.setIcon(createIcon(CONFIG.IMAGES.MARKER_BLUE));
             }
+        }
+        if (lastClickedDivide !== null) {
+            lastClickedDivide.setIcon(createIcon(CONFIG.IMAGES.MARKER_BLUE));
         }
         lastClickedMarker = data.target;
         map.panTo(currentMarker.getLngLat());
     }
     //璋冨畨鍗撳師鐢�
-    function showToast() {
+    function showToast(data) {
         // 璋冪敤 JavaScript 鎺ュ彛瀵硅薄鐨勬柟娉�
-        window.Android.showToast('Hello, Android!');
+        window.Android.showToast(data);
     }
     // 鍒ゆ柇涓や釜鍧愭爣鏄惁涓�鑷�
     function isEqualsLngLat(data1, data2) {
@@ -218,20 +229,11 @@
     function addMarker(id, lng, lat, name, isRed) {
         console.log("function銆嬨�嬨�嬨�嬨�媋ddMarker>>>id:" + id);
 
-        var iconUrl;
-        if (isRed) {
-            iconUrl = markerRedPath;
-        } else {
-            iconUrl = markerBluePath;
-        }
-        var icon = new T.Icon({
-            iconUrl: iconUrl,
-            iconSize: new T.Point(28, 28),
-            iconAnchor: new T.Point(14, 28)
-        });
+        const iconUrl = isRed ? CONFIG.IMAGES.MARKER_RED : CONFIG.IMAGES.MARKER_BLUE;
         let marker = new T.Marker(
-            new T.LngLat(lng, lat) // 鍒涘缓鏍囨敞
-            , { icon: icon });
+            new T.LngLat(lng, lat),
+            { icon: createIcon(iconUrl) }
+        );
         //娣诲姞鐐瑰嚮浜嬩欢
         marker.addEventListener("click", (data) => {
             addMarkerListener(id, data)
@@ -248,47 +250,180 @@
         label.setFontColor("#FFFFFF");
         label.setFontSize(10);
         marker.label = label;
-        if (isRed){
-            lastClickedMarker=marker;
+        if (isRed) {
+            lastClickedMarker = marker;
         }
         map.addOverLay(label);
         map.addOverLay(marker); // 灏嗘爣娉ㄦ坊鍔犲埌鍦板浘涓�
         return "addMarker鍔犺浇鎴愬姛 id:" + id
     }
-    //鏇存柊瀹氫綅鍧愭爣
+    //鏇存柊浣嶅潗鏍�
     function updateLocation(log, lat) {
-        let icon = new T.Icon({
-            iconUrl: locationIMGPath,
-            iconSize: new T.Point(20, 20),
-            iconAnchor: new T.Point(10, 10)
-        });
         if (locationMarker) {
             map.removeOverLay(locationMarker);
         }
         var newPoint = new T.LngLat(log, lat);
-        locationMarker = new T.Marker(newPoint, { icon: icon });
+        let icon = new T.Icon({
+            iconUrl: CONFIG.IMAGES.LOCATION,
+            iconSize: new T.Point(20, 20),
+            iconAnchor: new T.Point(10, 10)
+        });
+
+        locationMarker = new T.Marker(
+            newPoint,
+            { icon: icon }
+        );
         map.addOverLay(locationMarker);
         map.panTo(newPoint);
     }
     //淇濆瓨瀹氫綅鍧愭爣鐢熸垚杞ㄨ抗
     var path = [];
-    var lineLayer = new T.Polyline([], { color: '#ff4500', weight: 3, opacity: 0.8 });
+    var lineLayer = new T.Polyline([], { color: '#E9900A', weight: 3, opacity: 0.8 });
     let locationMarker;
     function updateInspectionLocation(log, lat) {
         var lastLat = lat;
-        // const intervalId = setInterval(() => {
-        // lastLat=lastLat+0.0001;
         var newPoint = new T.LngLat(log, lastLat);
         path.push(newPoint);
         lineLayer.setLngLats(path);
         map.addOverLay(lineLayer);
-        // 绉诲姩鍦板浘瑙嗚鍒版渶鍚庝竴涓綅缃�
+    }
 
-        // }, 500);
+
+    var pipePath = [];
+    var pipeLineLayer = new T.Polyline([], { color: '#E9900A', weight: 3, opacity: 0.8 });
+
+    // 绠$綉绾胯矾绠$悊
+    const PipelineManager = {
+        lines: [],  // 瀛樺偍鎵�鏈夊畬鎴愮殑绾胯矾
+        currentLine: {
+            points: [],  // 褰撳墠绾胯矾鐨勭偣
+            overlay: null  // 褰撳墠绾胯矾鐨勫浘灞傚璞�
+        },
+        
+        // 绾胯矾鏍峰紡閰嶇疆
+        style: {
+            color: '#1890FF',
+            weight: 3,
+            opacity: 0.8
+        },
+        
+        /**
+         * 娣诲姞鐐瑰埌绠$綉绾胯矾
+         * @param {number} lng 缁忓害
+         * @param {number} lat 绾害
+         * @param {boolean} isNewLine 鏄惁寮�濮嬫柊鐨勭嚎璺�
+         */
+        addPoint(lng, lat, isNewLine) {
+            if (isNewLine) {
+                this.finishCurrentLine();
+            }
+            
+            const point = new T.LngLat(lng, lat);
+            this.currentLine.points.push(point);
+            
+            if (this.currentLine.points.length > 1) {
+                this.updateCurrentLineDisplay();
+            }
+        },
+        
+        /**
+         * 鏇存柊褰撳墠绾胯矾鐨勬樉绀�
+         */
+        updateCurrentLineDisplay() {
+            if (!this.currentLine.overlay) {
+                // 鍒涘缓鏂扮殑绾胯矾鍥惧眰
+                this.currentLine.overlay = new T.Polyline(this.currentLine.points, this.style);
+                map.addOverLay(this.currentLine.overlay);
+            } else {
+                // 鏇存柊鐜版湁绾胯矾鐨勭偣
+                this.currentLine.overlay.setLngLats(this.currentLine.points);
+            }
+        },
+        
+        /**
+         * 瀹屾垚褰撳墠绾胯矾
+         */
+        finishCurrentLine() {
+            if (this.currentLine.points.length > 1) {
+                if (this.currentLine.overlay) {
+                    // 灏嗗綋鍓嶇嚎璺坊鍔犲埌瀹屾垚鍒楄〃
+                    this.lines.push(this.currentLine.overlay);
+                }
+            } else if (this.currentLine.overlay) {
+                // 濡傛灉鐐规暟涓嶈冻锛屾竻闄ゅ浘灞�
+                map.removeOverLay(this.currentLine.overlay);
+            }
+            
+            // 閲嶇疆褰撳墠绾胯矾
+            this.currentLine = {
+                points: [],
+                overlay: null
+            };
+        },
+        
+        /**
+         * 鏄剧ず鎵�鏈夌嚎璺�
+         */
+        showAll() {
+            this.lines.forEach(line => map.addOverLay(line));
+            if (this.currentLine.overlay) {
+                map.addOverLay(this.currentLine.overlay);
+            }
+        },
+        
+        /**
+         * 闅愯棌鎵�鏈夌嚎璺�
+         */
+        hideAll() {
+            this.lines.forEach(line => map.removeOverLay(line));
+            if (this.currentLine.overlay) {
+                map.removeOverLay(this.currentLine.overlay);
+            }
+        },
+        
+        /**
+         * 娓呴櫎鎵�鏈夌嚎璺�
+         */
+        clearAll() {
+            this.hideAll();
+            this.lines = [];
+            this.currentLine = {
+                points: [],
+                overlay: null
+            };
+        }
+    };
+
+    /**
+     * 娣诲姞绠$綉绾胯矾鐐�
+     */
+    function addPipeNetwork(lng, lat, isNewLine) {
+        PipelineManager.addPoint(lng, lat, isNewLine);
+    }
+
+    /**
+     * 鏄剧ず鎵�鏈夌缃戠嚎璺�
+     */
+    function showAllPipeLines() {
+        PipelineManager.showAll();
+    }
+
+    /**
+     * 闅愯棌鎵�鏈夌缃戠嚎璺�
+     */
+    function hideAllPipeLines() {
+        PipelineManager.hideAll();
+    }
+
+    /**
+     * 娓呴櫎鎵�鏈夌缃戠嚎璺�
+     */
+    function clearAllPipeLines() {
+        PipelineManager.clearAll();
     }
 
     var aginPath = [];
-    var oldLineLayer = new T.Polyline([], { color: '#ff4500', weight: 3, opacity: 0.8 });
+    var oldLineLayer = new T.Polyline([], { color: '#E9900A', weight: 3, opacity: 0.8 });
     //鏄剧ず鍥犲紓甯稿叧闂殑宸℃璁板綍
     function aginShowLocation(lng, lat) {
         // 璋冪敤 Android 鎻愪緵鐨勬帴鍙o紝鑾峰彇鏁版嵁
@@ -328,7 +463,7 @@
         isShowCenterPin = true;
         map.addEventListener("moveend", mapMoveEnd);
         window.Android.refreshCenter(map.getCenter().getLng(), map.getCenter().getLat());
-
+        return true;
     }
     //鍙栨秷淇敼瀹氫綅
     function cancelPin() {
@@ -344,9 +479,107 @@
 
 
     function mapMoveEnd(e) {
-        var center = e.target.getCenter();
-        console.log("lng:" + center.getLng() + "   lat:" + center.getLat())
-        window.Android.refreshCenter(center.getLng(), center.getLat());
+
+            const center = e.target.getCenter();
+            window.Android.refreshCenter(center.getLng(), center.getLat());
+
     }
 
+    // 娣诲姞闃叉姈鍑芥暟
+    function debounce(fn, delay) {
+        let timer = null;
+        return function () {
+            if (timer) clearTimeout(timer);
+            timer = setTimeout(() => {
+                fn.apply(this, arguments);
+            }, delay);
+        }
+    }
+
+
+
+    // 鍒涘缓marker鍥炬爣鐨勬柟娉曞彲浠ユ娊鍙栧嚭鏉ュ鐢�
+    function createIcon(iconUrl, size = 28) {
+        return new T.Icon({
+            iconUrl: iconUrl,
+            iconSize: new T.Point(size, size),
+            iconAnchor: new T.Point(size / 2, size)
+        });
+    }
+    //娣诲姞鍒嗘按鎴�
+    function addDivide(id, lng, lat, name) {
+        addDivide(id, lng, lat, name, false)
+    }
+    //娣诲姞鍒嗘按鎴�
+    function addDivide(id, lng, lat, name, isRed) {
+        console.log("function銆嬨�嬨�嬨�嬨�媋ddMarker>>>id:" + id);
+
+        const iconUrl = isRed ? CONFIG.IMAGES.DIVIDE_RED : CONFIG.IMAGES.DIVIDE_BLUE;
+        let marker = new T.Marker(
+            new T.LngLat(lng, lat),
+            { icon: createIcon(iconUrl) }
+        );
+        //娣诲姞鐐瑰嚮浜嬩欢
+        marker.addEventListener("click", (data) => {
+            addDivideListener(id, data)
+        });
+
+        let label = new T.Label({
+            text: `<div style='position:absolute;left:-50%;transform: translateX(-50%);'>${name}<div>`,
+            position: marker.getLngLat(),
+            offset: new T.Point(0, 8), // 璁剧疆鏍囨敞鏂囧瓧鐨勪綅缃�
+            opacity: 1, // 璁剧疆鏂囨湰鐨勬樉绀轰笉閫忔槑搴︼紙鑼冨洿0-1锛�
+        });
+        label.setBorderLine(0); // 璁剧疆鏂囨湰鐨勮竟妗嗙嚎瀹�
+        label.setBackgroundColor("transparent"); // 璁剧疆鏂囨湰鐨勮儗鏅壊锛堥�忔槑鑹诧級
+        label.setFontColor("#FFFFFF");
+        label.setFontSize(10);
+        marker.label = label;
+        if (isRed) {
+            lastClickedMarker = marker;
+        }
+        map.addOverLay(label);
+        map.addOverLay(marker); // 灏嗘爣娉ㄦ坊鍔犲埌鍦板浘涓�
+        return "addMarker鍔犺浇鎴愬姛 id:" + id
+    }
+    // 淇敼鐐瑰嚮鏍囨敞鐨勫浘鏍�
+    function chageDivideIcon(data) {
+        // 鐐瑰嚮鐨勬爣娉ㄦ敼涓虹孩鑹�
+        var currentMarker = data.target;
+        currentMarker.setIcon(createIcon(CONFIG.IMAGES.DIVIDE_RED));
+
+        if (lastClickedDivide !== null) {
+            if (!isEqualsLngLat(data.target.getLngLat(), lastClickedDivide.getLngLat())) {
+                lastClickedDivide.setIcon(createIcon(CONFIG.IMAGES.DIVIDE_BLUE));
+            }
+        }
+        if (lastClickedMarker !== null) {
+            lastClickedMarker.setIcon(createIcon(CONFIG.IMAGES.MARKER_BLUE));
+        }
+        lastClickedDivide = data.target;
+        map.panTo(currentMarker.getLngLat());
+    }
+
+    // 鐐瑰嚮鏍囨敞鐨勪簨浠�
+    function addDivideListener(id, data) {
+        // if (!isShowCenterPin) {
+        chageDivideIcon(data);
+        showDivideDetail(id);
+        // } else {
+        //     showToast("褰撳墠姝e湪淇敼閫変腑鍙栨按鍙g殑缁忕含搴︼紝瀹屾垚鎴栭��鍑哄悗鎵嶅彲閫夋嫨鍏朵粬锛�");
+        // }
+
+    }
+
+    // 璋冪敤鍘熺敓瀹夊崜鏂规硶鏄剧ず鍙栨按鍙h鎯�
+    function showDivideDetail(data) {
+        isShowDivideDetail = true;
+        window.Android.showDivideDetail(data);
+    }
+
+
+
+
+
+
 })();

--
Gitblit v1.8.0