【发布时间】:2017-04-30 10:38:04
【问题描述】:
问题:
我目前正在尝试在 D3 的地图上绘制陨石着陆数据。
我收到以下错误:未捕获的类型错误:
Cannot read property 'coordinates' of null index.html 89
我相信我获取 JSON 数据的方式是正确的,并且我使用 console.log 检查了 d.geometry 确实不为空。
我做错了什么?
代码:
<script>
// Map: https://codepen.io/manishgolcha/post/world-map-using-d3-js
var jMap = $(".map"),
height = jMap.height(),
width = jMap.width(),
mapJsonUrl = 'https://ucarecdn.com/8e1027ea-dafd-4d6c-bf1e-698d305d4760/world110m2.json',
svg = d3.select(".map").append("svg")
.attr("width", width)
.attr("height", height);
var getProjection = function() {
var scale = 1,
offset = [ width / 2, height / 2 ],
projection = d3.geoEquirectangular().scale( scale ).rotate( [0,0] ).center([0,5]).translate( offset ),
bounds = mercatorBounds( projection ),
scaleExtent;
scale = width / (bounds[ 1 ][ 0 ] - bounds[ 0 ][ 0 ]);
scaleExtent = [ scale, 10 * scale ];
projection
.scale( scaleExtent[ 0 ] );
return projection;
},
mercatorBounds = function(projection) {
var maxlat = 83,
yaw = projection.rotate()[ 0 ],
xymax = projection( [ -yaw + 180 - 1e-6, -maxlat ] ),
xymin = projection( [ -yaw - 180 + 1e-6, maxlat ] );
return [ xymin, xymax ];
};
d3.json(mapJsonUrl, function (error, worldJson) {
if (error) throw error;
var projection = getProjection(),
path = d3.geoPath().projection( projection );
svg.selectAll( 'path.land' )
.data( topojson.feature( worldJson, worldJson.objects.countries ).features )
.enter().append( 'path' )
.attr( 'class', 'land' )
.attr( 'd', path );
d3.json("https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/meteorite-strike-data.json", function(data) {
const geoData = data.features;
console.log(data.features[0].geometry);
svg.selectAll("circle")
.data(geoData)
.enter()
.append("circle")
.attr("cx", (d) => projection([d.geometry.coordinates[0], d.geometry.coordinates[1]])[0])
.attr("cy", (d) => projection([d.geometry.coordinates[0], d.geometry.coordinates[1]])[1])
.attr("r", d.properties.mass)
.style("fill", "orange")
.style("opacity", 0.8);
});
});
</script>
【问题讨论】:
标签: javascript json d3.js