javascript - 多边形未绘制 // 表示变量不存在,但控制台中没有返回错误
问题描述
我正在尝试在地图上显示多边形,我在此地图上使用的数据来自带有数组的数据库,当我得到这些坐标console.log
时,我尝试使用的完整坐标是这些[obj[2][0].geometry.coordinates[0][0][0]
4.87896
0: (2) ["4.87896", "52.367477"]
1: (2) ["4.87896", "52.379843"]
2: (2) ["4.918785", "52.379843"]
3: (2) ["4.918785", "52.367477"]
4: (2) ["4.87896", "52.367477"]
以及我如何尝试使多边形显示在地图上的代码
let map = L.map('map').setView([52.37, 4.90], 13);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 14,
}).addTo(map);
var polygon = L.polygon([
[obj[2][0].geometry.coordinates[0][0][0], obj[2][0].geometry.coordinates[0][0][1]],
[obj[2][0].geometry.coordinates[0][1][0], obj[2][0].geometry.coordinates[0][1][1]],
[obj[2][0].geometry.coordinates[0][2][0], obj[2][0].geometry.coordinates[0][2][1]],
[obj[2][0].geometry.coordinates[0][3][0], obj[2][0].geometry.coordinates[0][3][1]],
[obj[2][0].geometry.coordinates[0][4][0], obj[2][0].geometry.coordinates[0][4][1]]
]).addTo(map);
多边形没有显示在地图上,但是console
当我输入它polygon
时也没有返回错误。console
VM94:1 未捕获的 ReferenceError:多边形未定义于:1:1
我使用的所有链接
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css"
integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
crossorigin=""/>
<!-- Make sure you put this AFTER Leaflet's CSS -->
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js"
integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg=="
crossorigin=""></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.4.2/leaflet.draw.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.4.2/leaflet.draw.js"></script>
编辑:js的一个bin
解决方案
经过一些测试,我发现坐标只接受numbers
,所以在这种情况下它必须是Number([obj[2][0].geometry.coordinates[0][0][0])
,然后画得很好
推荐阅读
- python - 路径之和 III - Leetcode
- powershell - 电源外壳解决方案检查器脚本问题
- python - 如何自动化从具有多个部分的 CSV 文件中读取的脚本?
- matter.js - 如何从身体中获得约束
- c - 了解嵌入式 C 中的 I/O 内存位置
- postgresql - 可以在端口 22 上连接到 Azure VM,但不能在端口 5111 上连接
- javascript - 如何使用带有反应本机和javascript的特定协议处理远程服务器的加密请求和答案
- git - Git混淆命令进行部署
- php - 如何在 Apache2.4 中打开“Pathinfo”
- google-sheets - 如何在同一张工作表中使用现有的谷歌电子表格(带有公式和格式)作为模板