带有 D3js 的引脚标记
Pin Markers with D3js
我正在实现一张地图,该地图将按纬度和经度进行标记。我正在使用 D3Js 从 topojson 文件创建 svg。
这是实现:
var places = [
{
name: "Longo, England",
location: {
latitude: 51.5,
longitude: -0.116667
}
},
{
name: "Dublin, Irland",
location: {
latitude: 53.428590,
longitude: -6.188024
}
}
]
var margin = {top: 10, left: 10, bottom: 10, right: 10};
var width = parseInt(d3.select('#map').style('width'));
width = width - margin.left - margin.right;
var mapRatio = 1;
var height = width * mapRatio;
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("data/uk.json", function(error, uk) {
if (error) return console.error(error);
var subunits = topojson.feature(uk, uk.objects.subunits);
var projection = d3.geo.mercator()
.center([0, 55.4])
.rotate([4.4, 0])
.scale(500)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
svg.append("path")
.datum(subunits)
.attr("d", path)
.attr("class", "subunit-boundary IRL");
svg.selectAll(".subunit")
.data(topojson.feature(uk, uk.objects.subunits).features)
.enter().append("path")
.attr("class", function(d) { return "subunit " + d.id; })
.attr("d", path);
svg.selectAll(".pin")
.data(places)
.enter()
.append('image')
.attr('class', 'datamaps-pin')
.attr('xlink:href', 'http://a.tiles.mapbox.com/v3/marker/pin-m+7e7e7e@2x.png')
.attr('height', 40)
.attr('width', 40)
.attr("transform", function(d) {
return "translate(" + projection([
d.location.longitude,
d.location.latitude
]) + ")"
});
但是,引脚已错位。
当我更改简单点的标记时,它应该在正确的位置
svg.selectAll(".pin")
.data(places)
.enter().append("circle", ".pin")
.attr("r", 5)
.attr("transform", function(d) {
return "translate(" + projection([
d.location.longitude,
d.location.latitude
]) + ")"
});
如果未指定任何 x 或 y,则以原点 (x,y = 0) 为中心绘制一个圆。但是,如果未指定 x 或 y,则会在原点绘制图像的左上角。
要获得与圆圈相同的效果,请添加
.attr("x", -20)
.attr("y", -20)
到图钉的图像版本。
相关文章:
- 谷歌地图自定义引脚
- ScrollMagic引脚间隔太大
- 带有 D3js 的引脚标记
- 尝试了解重新渲染引脚函数在做什么
- 使用johnny-five和arduino读取输入引脚
- 如何降低谷歌地图的整体亮度,使引脚更显眼
- Bing Maps v7-多边形会阻止多边形内(后面)显示的引脚的鼠标悬停事件
- 谷歌地图在显示地图引脚之前需要页面刷新
- 如何在Bing Maps AJAX Control 7.0版中将引脚添加到两个不同的实体层,然后清除特定实体层上的引脚
- 在ArcGIs中创建类似谷歌的数字引脚
- AngularJS/NgMap-在引脚设置更改时重新输入映射
- ScrollMagic引脚在TimelineMax中不起作用
- 如何更改传单指令上的默认图标引脚
- 谷歌地图-默认引脚仍然显示和模糊自定义图标
- GMAPS API事件处理引脚点
- 定心谷歌地图与javascript从引脚
- 从加载的KMZ文件中删除所有引脚(标记)
- 我如何使用GMaps API来显示默认为用户的HTML5地理位置的可拖动引脚?
- 可拖动引脚不更新坐标在谷歌地图API
- 生成一个随机的四位数引脚,最后一个数字总是零