Javascript 为什么不在屏幕上渲染地图?I';我正在使用D3库,我的geojson文件工作正常 常数宽度=960; 常数高度=500; const svg=d3.select('svg') .attr(“宽度”,宽度) .attr(“高度”,高度); const projection=d3.geoMercator().fitSize([width,height],'resources/lebanian map.geojson'); const path=d3.geoPath().projection(projection); append('svg') .selectAll('路径') .data('resources/learnian map.geojson'.features) .输入() .append('路径') .attr('d',路径) .样式(“填充”、“红色”) .样式(“笔划宽度”、“1”) .风格(“笔划”、“黑色”);
它只是没有显示任何东西。 我正在使用WebStorm和最新版本的d3,这就是为什么我想v4中添加了fitSize函数的原因Javascript 为什么不在屏幕上渲染地图?I';我正在使用D3库,我的geojson文件工作正常 常数宽度=960; 常数高度=500; const svg=d3.select('svg') .attr(“宽度”,宽度) .attr(“高度”,高度); const projection=d3.geoMercator().fitSize([width,height],'resources/lebanian map.geojson'); const path=d3.geoPath().projection(projection); append('svg') .selectAll('路径') .data('resources/learnian map.geojson'.features) .输入() .append('路径') .attr('d',路径) .样式(“填充”、“红色”) .样式(“笔划宽度”、“1”) .风格(“笔划”、“黑色”);,javascript,d3.js,geojson,Javascript,D3.js,Geojson,它只是没有显示任何东西。 我正在使用WebStorm和最新版本的d3,这就是为什么我想v4中添加了fitSize函数的原因 提前感谢您您的数据似乎是一个字符串:'resources/learnish map.geojson'-这不是geojson,也没有features属性,您需要使用d3.json('resources/learnish map.geojson')加载文件。然后(函数(geojson){/*在这里使用geojson做些事情*/})我试过了,但还是不行@AndrewReidYou
提前感谢您您的数据似乎是一个字符串:
'resources/learnish map.geojson'
-这不是geojson,也没有features属性,您需要使用d3.json('resources/learnish map.geojson')加载文件。然后(函数(geojson){/*在这里使用geojson做些事情*/})
我试过了,但还是不行@AndrewReidYou还需要在.then()函数中设置投影fitSize,因为它也使用geojson。可能适用:此外,您需要在服务器上运行脚本,否则由于浏览器限制,d3.json无法访问geojson。谢谢@AndrewReid工作正常
<script>
const width = 960;
const height = 500;
const svg = d3.select('svg')
.attr("width", width)
.attr("height", height);
const projection = d3.geoMercator().fitSize([width, height], 'resources/lebanese-map.geojson');
const path = d3.geoPath().projection(projection);
svg.append('svg')
.selectAll('path')
.data('resources/lebanese-map.geojson'.features)
.enter()
.append('path')
.attr('d', path)
.style("fill", "red")
.style("stroke-width", "1")
.style("stroke", "black");
</script>