我一直热衷于为Namibia.But开发一个choropleth地图,我发现了两个有趣的工具。leaflet和D3,虽然leaflet有明确的指令来实现我做的事情,但它在功能上并不符合我想做的事情。这就是D3Geo的用武之地。我已经把所有的东西都设置好了,除了下面这个函数来设置我的投影。
var projection = d3.geo.conicConformal()
.rotate([, 0])
.center([0, 0])
.parallels([ , ])
.scale(1000) 是不是没有简单地添加坐标的功能,就像下面的宣传单功能中那样。对于我们这些不是以地心为中心的人。
var map = L.map('mapid').setView([-22.26,16.52], 5);如果没有,可以有人指导我如何转换坐标(-22.26,16.52 ),以显示使用d3.geo.conicConformal()。
发布于 2016-09-12 06:13:43
如果它没有解决你的问题,请纠正我(也许你可以提供一个最小的例子来说明你被困在哪里,例如使用JSFiddle ),但是如果我理解得很好,你想要在你的国家的扩展上移动/缩放/居中显示的图像。下面是一个这样做的例子(为了保持一致性,我还添加了一些关于如何添加层的代码):
// Define the projection you want to use,
// setting scale and translate to some starting values :
var projection = d3.geoConicConformal()
.translate([0, 0])
.scale(1)
var layer_name = "your_layer_name";
var geo_features = topojson.feature(topoObj, topoObj.objects[layer_name]).features;
// Define the path generator :
var path = d3.geoPath().projection(projection);
var width = 800,
height = 600;
// This is the main svg object on which you are drawing :
var map = d3.select("body").append("div")
.style("width", width + "px")
.style("height", height + "px")
.append("svg")
.attr("id", "svg_map")
.attr("width", width)
.attr("height", height);
// Add you layer to the map
map.append("g").attr("id", layer_name)
.attr("class", "layers")
.selectAll("path")
.data(geo_features)
.enter().append("path")
.attr("d", path)
.attr("id", (d,i)=> "feature_" + i)
.styles({"stroke": "rgb(0, 0, 0)", "fill": "beige")
// Where the job is done :
scale_to_layer(layer_name)
function scale_to_layer(name){
var bbox_layer = undefined;
// use all the paths of the layer (if there is many features)
// to compute the layer bbox :
map.select("#"+name).selectAll('path').each(function(d, i){
var bbox_path = path.bounds(d);
if(bbox_layer === undefined){
bbox_layer = bbox_path;
}
else {
bbox_layer[0][0] = bbox_path[0][0] < bbox_layer[0][0]
? bbox_path[0][0] : bbox_layer[0][0];
bbox_layer[0][1] = bbox_path[0][1] < bbox_layer[0][1]
? bbox_path[0][1] : bbox_layer[0][1];
bbox_layer[1][0] = bbox_path[1][0] > bbox_layer[1][0]
? bbox_path[1][0] : bbox_layer[1][0];
bbox_layer[1][1] = bbox_path[1][1] > bbox_layer[1][1]
? bbox_path[1][1] : bbox_layer[1][1];
}
});
// Compute the new scale param, with a little space (5%) around the outer border :
var s = 0.95 / Math.max((bbox_layer[1][0] - bbox_layer[0][0]) / width,
(bbox_layer[1][1] - bbox_layer[0][1]) / height);
// Compute the according translation :
var t = [(width - s * (bbox_layer[1][0] + bbox_layer[0][0])) / 2,
(height - s * (bbox_layer[1][1] + bbox_layer[0][1])) / 2];
// Apply the new projections parameters :
projection.scale(s)
.translate(t);
// And redraw your paths :
map.selectAll("g.layer").selectAll("path").attr("d", path);
};还要注意,本例使用了d3 v4 (但在本例中,除了geoPath和geoConicConformal的命名外,它并没有太多变化)
https://stackoverflow.com/questions/39440033
复制相似问题