投影([lat,lng])一直返回null
projection([lat,lng]) keeps returning null
我正试图使用反照率Usa投影将GeoJSON文件中的几个点映射到地图上。我的代码如下,但未能将cx
和cy
属性添加到circle
元素:
var width = 960, height = 500;
// set projection
var projection = d3.geo.albersUsa()
.scale(1000)
.translate([width / 2, height / 2]);
// create path variable
var path = d3.geo.path().projection(projection);
d3.json("us.json", function(error, topo) {
states = topojson.feature(topo, topo.objects.states).features
// create svg variable
var svg = d3.select("#body").append("svg")
.attr("width", width)
.attr("height", height);
// add states from topojson
svg.append("g").attr("class", "feature").selectAll("path")
.data(states).enter()
.append("path")
.attr("class", "feature")
.attr("d", path);
// put border around states
svg.append("g").attr("class", "mesh").append("path")
.datum(topojson.mesh(topo, topo.objects.states, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
d3.json("data/bhutan.json", function(error, bhutan) {
// add circles to svg
svg.selectAll("circle")
.data(bhutan.features)
.enter()
.append("circle")
.attr("class", "bhutan")
.attr("cx", function(d) {
var longitude = d.geometry.coordinates[0];
var latitude = d.geometry.coordinates[1];
return projection(latitude);
})
.attr("cy", function(d) {
var longitude = d.geometry.coordinates[0];
var latitude = d.geometry.coordinates[1];
return projection(longitude);
})
.attr("r", "8px");
});
还有我的JSON:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [39.13, -84.48845]
},
"properties": {
"state": "Ohio",
"city": "2816 Cincinnati",
"arrivals": "1"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [32.44874, -99.73314]
},
"properties": {
"state": "Texas",
"city": "Abilene",
"arrivals": "178"
}
}, {
…
}]
}
有人能帮我弄清楚我做错了什么吗?
您错误地调用了投影——需要给它一对坐标:
.attr("cx", function(d) {
var longitude = d.geometry.coordinates[1];
var latitude = d.geometry.coordinates[0];
return projection([longitude, latitude])[0];
})
.attr("cy", function(d) {
var longitude = d.geometry.coordinates[1];
var latitude = d.geometry.coordinates[0];
return projection([longitude, latitude])[1];
})
相关文章:
- 节点导出返回一个空对象
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 控制台返回var不是't定义,但它是
- 从函数返回角度承诺
- Javascript返回值只在循环中返回一次
- 从控制器返回后Ajax启动事件激发
- 对象HTMLcollection[0]一直返回未定义
- 值一直返回为 0,不知道为什么
- 数据 ID 一直未定义返回
- 我的本地站点中的 JavaScript 搜索函数一直返回未定义
- 投影([lat,lng])一直返回null
- 一直返回[objecthtmlspanelement]?(Javascript)
- 使用javascript解析JSON对象时一直返回'undefined'对于名称中带有点的对象
- ajaxForm插件一直重定向到操作页面,忽略返回false;
- CoffeeScript一直触发错误,当我得到一个200响应返回
- ASP的GetElementById.NET Control一直返回'null'
- 函数一直返回未定义
- 函数一直返回真,应该返回假
- REST返回404表示未找到任何记录,但错误一直显示在浏览器控制台中