在使用d3.js制作的地图上显示图标/图像,而不是圆圈/路径
Display icons/image instead of circle/path on map made with d3.js
我可以使用d3.js成功地在openlayers底图上显示一些点,但是我想实际显示图标(目前使用png图标)而不是svg点。
是否可以加载png/jpg/svg图像到地图使用d3.js?
到目前为止,我可以将图标直接加载到svg上,但所有标记都位于同一位置,所以我认为我在正确转换坐标时遇到了问题。
var feature = svg.selectAll("path")
.data(amenities.features)
.enter()
.append("svg:image")
.attr("xlink:href", "maki/renders/post-18@2x.png")
.attr("x", function(d, i) {return amenities.features[i].geometry.coordinates[0]})
.attr("y", function(d, i) {return amenities.features[i].geometry.coordinates[1]})
.attr("width", "20")
.attr("height", "20")
.attr("class", "amenity");
以前,我已经能够使用"模式"创建一个带有图像背景的svg来显示图像,所以这也是一种可能性,但我无法翻译代码以使用d3的地理方面。
我知道我正在写的图标,目前的'svg'div,所以他们不能正确地转换,当我放大和缩小。我的目标是将图像写入'g'div,如在代码中:
var feature = g.selectAll("path")
但是当我使用这一行时,元素出现在文档上,但是图标实际上没有呈现在地图上
这里有几个问题。首先,我不确定您是否完全掌握d3选择是如何工作的,正如您将amenities.features
绑定到您的选择,然后通过索引访问x
和y
属性所表明的那样。有关这方面的更多详细信息,请参阅选择如何工作。此外,您需要通过地理投影函数将特征的地理坐标转换为屏幕坐标。这应该使您接近:
// more projections: https://github.com/d3/d3-geo-projection/
var projection = d3.geoAlbers();
var amenities = svg.selectAll('.amenities')
.data(amenities.features);
amenities.enter().append('image');
amenities
.attr("class", "amenities")
.attr("xlink:href", "maki/renders/post-18@2x.png")
// The data is already bound so use it instead of the index. Als,
// you need to translate geo coordinates to screen coordinates by
// passing them through your projection function.
.attr("x", function(d,i) {return projection(d.geometry.coordinates)[0];})
.attr("y", function(d,i) {return projection(d.geometry.coordinates)[1];})
.attr("width", "20")
.attr("height", "20")
我不认为我使用的组是合适的但我认为关键是有transform然后翻译的东西在那里。
我的例子是http://bl.ocks.org/mpmckenna8/b87df1c44243aa1575cb。
但是因为我没有正确地使用分组,我不知道图标是否会像你想要的那样处理缩放。在我的例子中,我只是将图像附加到我创建的圆圈中。
.attr("transform", function(d) { return "translate(" + projection(d.geometry.coordinates) + ")"; })
.attr('opacity',.3)
.attr('fill', '#fad959')
相关文章:
- 加载图像时加载图标
- 如何制作Yii框架的linkHtmlOptions's的CGridView显示为图像图标,单击时会触发javas
- Google Places API显示雷达搜索中的位置图标/图像
- 访问标记图标的图像数组元素
- 使用KendoUI网格,您将如何将可选图像或图标添加到行模板中
- 如何添加可拖动的“;添加图像”;jquery中的图标
- 如何将日期选择器放在图像图标上
- 如何在gmaps.js上添加标记图像图标
- Kendo TreeView-只向没有子节点的父节点添加删除图像图标
- 单击我们在 css 中添加的图像图标时的下拉列表
- 无法在网页上显示的 HTML 段落上叠加图像图标
- 将图像/图标添加为数据表中的行值
- dataTables如何添加排序图像/图标
- Javascript加载/旋转图像/图标,同时加载搜索结果
- 使用Ajax预览图像:返回一个损坏的图像图标
- Javascript从数据动态创建图像/图标(标记)
- 在d3中添加图像/图标而不是svg形状
- 用文字代替破碎的图像图标
- 在静态图像上绘制图像图标
- 是LeafletJS异步的,当它加载图像图标时