我想合成两个不同的D3模板,两者都是由Mike Bostock编写的,
这个想法是将城市数据从1渲染为2的正交地球上的点,就像.kml文件一样。然后,用户可以旋转地球仪,并在需要时点击一个点。
这是我的第一次尝试,但失败了 问题是我无法在地图上获得投射点数。 这是因为我不明白如何操纵csv数据来呈现我也喜欢它的方式。现在,输出是一次成功的世界巡回演出,以及此巡演下方的积分显示。它们都在页面正文中呈现,这很令人困惑,因为我试图将CSV代码嵌入到显示巡视图的ready函数中。 我只是无法将这些点附加到地图上。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
h1 {
position: absolute;
top: 280px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 18px;
text-align: center;
width: 960px;
}
</style>
<h1></h1>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.orthographic()
.scale(248)
.clipAngle(90);
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height", height);
var c = canvas.node().getContext("2d");
var path = d3.geo.path()
.projection(projection)
.context(c);
var title = d3.select("h1");
queue()
.defer(d3.json, "world-110m.json")
.defer(d3.tsv, "world-country-names.tsv")
.await(ready);
function ready(error, world, names) {
var globe = {type: "Sphere"},
land = topojson.feature(world, world.objects.land),
countries = topojson.feature(world, world.objects.countries).features,
borders = topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }),
i = -1,
n = countries.length;
countries = countries.filter(function(d) {
return names.some(function(n) {
if (d.id == n.id) return d.name = n.name;
});
}).sort(function(a, b) {
return a.name.localeCompare(b.name);
});
(function transition() {
d3.transition()
.duration(1250)
.each("start", function() {
title.text(countries[i = (i + 1) % n].name);
})
.tween("rotate", function() {
var p = d3.geo.centroid(countries[i]),
r = d3.interpolate(projection.rotate(), [-p[0], -p[1]]);
return function(t) {
projection.rotate(r(t));
c.clearRect(0, 0, width, height);
c.fillStyle = "#bbb", c.beginPath(), path(land), c.fill();
c.fillStyle = "#f00", c.beginPath(), path(countries[i]), c.fill();
c.strokeStyle = "#fff", c.lineWidth = .5, c.beginPath(), path(borders), c.stroke();
c.strokeStyle = "#000", c.lineWidth = 2, c.beginPath(), path(globe), c.stroke();
};
})
.transition()
.each("end", transition);
})();
// copied this code from the point / city example. I don't know how to integrate it into the world tour.
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var g = svg.append("g");
d3.csv("cities.csv", function(error, data) {
g.selectAll("circle")
.data(data)
.enter()
.append("a")
.attr("xlink:href", function(d) {
return "https://www.google.com/search?q="+d.city;}
)
.append("circle")
.attr("cx", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("cy", function(d) {
return projection([d.lon, d.lat])[1];
})
.attr("r", 5)
.style("fill", "red");
});
}
</script>
所以基本上我可以使用一些帮助将城市数据导入世界旅游地图。
感谢。