javascript – D3:使用topojson的地图无法正常渲染
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – D3:使用topojson的地图无法正常渲染,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1914字,纯文字阅读大概需要3分钟。
内容图文
我正试图用topojson文件中的d3绘制svg地图,但我所得到的只是乱糟糟的线条.
我正在使用我在http://www.tnoda.com/blog/2013-12-07上找到的大部分代码.当我使用该网站的topojson文件时,一切正常.我认为问题可能在我的topojson文件中,但是当我在mapshaper中导入它时,我得到了正常的地图.
plnkr:http://plnkr.co/edit/TYiT5AoI29nEHC3Fre6D?p=preview
这是我的代码:
的index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="//code.jquery.com/jquery-2.0.0.js"></script>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
</head>
<body>
<div id="map"></div>
<script src="script.js"></script>
</body>
</html>
的script.js
var m_width = $("#map").width(),
width = 800,
height = 500
var projection = d3.geo.mercator()
.scale(105)
.translate([width / 2, height / 1.5]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#map").append("svg")
.attr("width", m_width)
.attr("height", m_width * height / width);
var g = svg.append("g");
d3.json("zupanije.max.topo.json", function(error, us) {
g.append("g")
.attr("id", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter()
.append("path")
.attr("id", function(d) { return d.id; })
.attr("d", path)
});
styles.css的
#map {
background-color: #fff;
border: 1px solid #ccc;
}
.background {
fill: none;
pointer-events: all;
}
#states{
cursor: pointer;
fill: #cde;
stroke: #fff;
stroke-linejoin: round;
stroke-linecap: round;
}
#states .active {
fill: #89a;
}
pre.prettyprint {
border: 1px solid #ccc;
margin-bottom: 0;
padding: 9.5px;
}
解决方法:
我遇到了完全相同的问题,花了几个小时在命令行中将我的SHP文件重新转换为GeoJSON / Topojson,具有不同的设置.解决方案非常简单!
– 在这里获取QGIS:https://www.qgis.org/en/site/forusers/download.html
– 打开您的SHP文件或GeoJSON文件
– 选择要导出的图层
– 转到图层>另存为
– 格式:Geojson
– CSR:WGS 84,EPSG:4326
– 保存.
请享用!
内容总结
以上是互联网集市为您收集整理的javascript – D3:使用topojson的地图无法正常渲染全部内容,希望文章能够帮你解决javascript – D3:使用topojson的地图无法正常渲染所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。