| | |
| | | var map = new AMap.Map('container',{ |
| | | // 将 polygons 数组声明提前并确保全局唯一 |
| | | var polygons = []; |
| | | var map = new AMap.Map('container', { |
| | | resizeEnable: true, |
| | | zoom:15, |
| | | center: [102.2667, 27.8833] // 西昌坐标 |
| | | zoom: 4, // 使用更小的缩放级别以适应大范围坐标 |
| | | center: [77.3833, 34.8833] // 使用更居中的坐标 |
| | | }); |
| | | setCenterCoordinate(); |
| | | function setCenterCoordinate() { |
| | |
| | | map.setCenter(center) |
| | | } |
| | | } |
| | | // 修改 setElectricFence 函数来支持单个坐标数据 |
| | | function setElectricFence() { |
| | | console.log("检查 coordinateData:", typeof coordinateData, coordinateData); |
| | | if (typeof coordinateData !== 'undefined' && coordinateData) { |
| | | console.log("开始绘制多边形,坐标数据:", coordinateData); |
| | | var path = convertData(coordinateData); |
| | | console.log("转换后的路径:", path); |
| | | var polygon = new AMap.Polygon({ |
| | | path: path, |
| | | strokeWeight: 2, |
| | | fillOpacity: 0.4, |
| | | fillColor: '#00b0ff', |
| | | strokeColor: '#80d8ff' |
| | | }); |
| | | map.add(polygon); |
| | | polygons.push(polygon); |
| | | |
| | | // 使用 setFitView 确保多边形可见 |
| | | map.setFitView([polygon]); |
| | | |
| | | console.log("多边形已添加到地图"); |
| | | return; |
| | | } |
| | | |
| | | // 原有的处理多个位置的逻辑保持不变 |
| | | var locationsStr = $("#locationList1").val(); |
| | | if ("" != locationsStr && null != locationsStr) { |
| | | var locations = JSON.parse(locationsStr); |
| | | for (let i = 0; i < locations.length; i++) { |
| | | console.log(locations[i]) |
| | | var gid = locations[i].coordinate |
| | | if (gid == null || gid == "") { |
| | | continue; |
| | |
| | | strokeColor: '#80d8ff' |
| | | }); |
| | | map.add(polygon); |
| | | polygons.push(polygon); |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | |
| | | var district = null; |
| | | var polygons=[]; |
| | | |
| | | function drawBounds() { |
| | | // 在编辑模式下不执行行政区划绘制 |
| | | if (typeof coordinateData !== 'undefined' && coordinateData) { |
| | | return; |
| | | } |
| | | |
| | | if(value == ''){ |
| | | value = '北京市'; |
| | | value = '西昌市'; |
| | | } |
| | | |
| | | //加载行政区划插件 |
| | |
| | | //行政区查询 |
| | | district.setLevel(document.getElementById('level').value) |
| | | district.search(value, function(status, result) { |
| | | map.remove(polygons)//清除上次结果 |
| | | polygons = []; |
| | | // 只清除行政区划多边形,保留电子围栏 |
| | | var tempPolygons = []; |
| | | for (var i = 0; i < polygons.length; i++) { |
| | | // 保留用户绘制的电子围栏(通过判断是否在objs中) |
| | | var isUserPolygon = false; |
| | | for (var j = 0; j < objs.polygon.length; j++) { |
| | | if (polygons[i] === objs.polygon[j]) { |
| | | isUserPolygon = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!isUserPolygon) { |
| | | tempPolygons.push(polygons[i]); |
| | | } |
| | | } |
| | | map.remove(tempPolygons); |
| | | |
| | | var bounds = result.districtList[0].boundaries; |
| | | if (bounds) { |
| | | for (var i = 0, l = bounds.length; i < l; i++) { |
| | |
| | | fillColor: '#80d8ff', |
| | | strokeColor: '#0091ea' |
| | | }); |
| | | map.add(polygon); |
| | | polygons.push(polygon); |
| | | } |
| | | } |
| | | map.add(polygons) |
| | | map.setFitView(polygons);//视口自适应 |
| | | // 只对行政区划多边形进行视野调整 |
| | | if (bounds && bounds.length > 0) { |
| | | map.setFitView(polygons.slice(polygons.length - bounds.length)); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | document.getElementById('find').onclick = function () { |
| | | value = document.getElementById('district').value; |
| | | if(value == ''){ |
| | | value = "北京市"; |
| | | value = "西昌市"; |
| | | } |
| | | drawBounds() |
| | | }; |
| | |
| | | if (e.keyCode === 13) { |
| | | value = document.getElementById('district').value; |
| | | if(value == ''){ |
| | | value = "北京市"; |
| | | value = "西昌市"; |
| | | } |
| | | drawBounds(); |
| | | return false; |
| | |
| | | |
| | | document.getElementById('clear').onclick = function(){ |
| | | map.remove(overlays) |
| | | map.remove(polygons); |
| | | overlays = []; |
| | | coordinate = ''; |
| | | objs.circle = []; |
| | |
| | | editor.close(); |
| | | } |
| | | } |
| | | |
| | | $(function () { |
| | | console.log("coordinateData:", coordinateData); // 添加调试日志 |
| | | setElectricFence() |
| | | }) |