我一直在遵循“让我们绘制一个地图”教程https://bost.ocks.org/mike/map/,但是在教程的中间,绘制路径失败了,而有关topojson.feature()的错误并不是一个函数。
script.js:14 Uncaught : topojson.feature不是函数(…)
(匿名函数)@ script.js:14
(匿名函数)@ d3.min.js:6
打电话@ d3.min.js:6
E@ d3.min.js:6
script.js:
var width = 960,
height = 1160;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("js/uk.json", function(error, uk) {
if (error) return console.error(error);
console.log(uk);
svg.append("path")
.datum(topojson.feature(uk, uk.objects.subunits))
.attr("d", d3.geo.path().projection(d3.geo.mercator()));
});
HTML包含脚本:
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.0/d3.min.js"></script>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>Map test</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script src='js/jquery.js'></script>
<script src='js/d3.min.js' charset="utf-8"></script>
<script src='js/topojson.js'></script>
<script src="js/script.js" type="text/javascript"></script>
</body>
</html>
最初,我怀疑这个问题与我转换过的Pathfile-GeoJSON-TopoJSON的topojson文件有关,我在遵循本教程时将其命名为uk.js,用于测试目的。在这里的答复中提到的另一个提示:https://github.com/topojson/topojson/issues/236在更改问题后保持不变,并在替换了uk.json之后确认了我使用了:https://bost.ocks.org/mike/map/uk.json
发布于 2016-12-08 18:22:04
您丢失了topojson客户端文件,这些文件已经从v2中的基本topojson代码中分离出来。如果您在代码中包含了以下脚本,那么它应该可以工作:
<script src="https://unpkg.com/topojson-client@2"></script>https://stackoverflow.com/questions/41046024
复制相似问题