javascript - 如何将坐标传递给 Leaflet 中的 LatLng 函数
问题描述
我试图弄清楚如何将坐标传递给传单中的 L.LatLng 函数,以便它可以映射坐标。
我可以成功加载数据,它看起来像这样:
//This is the structure of the geojson data, as an example
var SanFranciscoData = {"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Point","coordinates":[-96.97593699999999,32.889954000000046]}
//Load in the geojson
d3.json("data/dataPoints.json", function(SFData) {
var SFData = SanFranciscoData.features
})
//pass in coordinates to the L.LatLng leaflet function
SFData.forEach(function(d) {
d.latLong = new L.LatLng(d.features.geometry.coordinates[1],
d.features.geometry.coordinates[0]);
})
正如我在示例中看到的那样,我也尝试过:
var coords = SFData.feature.geometry.coordinates;
上述两种方法都给出相同的结果:坐标未定义。我究竟做错了什么?我不确定如何使用对象表示法访问坐标数组以访问纬度/经度。
解决方案
好的..这有效:
SFData.forEach(function(d) {
var coords = d.geometry.coordinates
console.log(coords)
d.latLong = new L.LatLng(coords[1],
coords[0]);
})
似乎需要使用 .forEach 函数定义坐标!
推荐阅读
- android - 计算检测到的人的距离
- c# - 无法计算两个数字之间的百分比差异
- c++ - 将编译时定义大小的数组初始化为常量表达式
- c# - 在 SSIS 的脚本任务编辑器中出现错误
- python - 在扑克游戏中表示手牌并识别手牌组合
- sql - 如何在 Bigquery 中使用 SQL 查询设置触发器?
- excel - 有没有一种简单的方法可以将 2 变量设置为范围函数
- python - find_all 正在捕获我想要的标签,但 find_all_previous 没有(看起来应该是)
- c++ - 如何检查一个库是否可以被特定的 g++ 版本编译?
- node.js - Mongoose.connect 不是从其他文件导出后的函数