【发布时间】:2020-10-05 21:57:24
【问题描述】:
我正在做一个项目,其中有一个如下所示的 JSON:
[
{
"lat": 53.1521596106757,
"lon": -0.486577431632087,
"size": 3598,
"field": "TestField",
"variety": "TestVariety",
"count": 67
},
{
"lat": 53.1521596106757,
"lon": -0.486287281632087,
"size": 4077,
"field": "TestField",
"variety": "TestVariety",
"count": 73
}
]
我正在尝试使用 count 添加文本,但使用以下代码没有看到预期的结果:
let testField = new google.maps.LatLng(53.1519, -0.4895);
const map = new google.maps.Map(d3.select('#map').node(), {
zoom: 17,
center: testField,
mapTypeId: 'satellite',
streetViewControl: false,
mapTypeControl: false,
});
//colour scale
// const colorScale = d3.scaleSequential(d3.interpolateBlues);
d3.json('/small_example.json')
.then(data => {
let countInfo = data.map(function (testVariety) {
return testVariety.count;
})
// colorScale.domain(data.map(d => d.countInfo))
//create overlay
const overlay = new google.maps.OverlayView();
// Add the container when the overlay is added to the map.
overlay.onAdd = function () {
const layer = d3.select(this.getPanes().overlayLayer).append('div')
.attr('class', 'panes');
// Draw each marker as a separate SVG element.
overlay.draw = function () {
const projection = this.getProjection(),
padding = 10;
const marker = layer.selectAll('svg')
.data(d3.entries(data))
.each(transform) // update existing markers
.enter().append('svg')
.each(transform)
.attr('class', 'marker')
//add a rect
marker.append('rect')
.attr('height', 15)
.attr('width', 15)
// .style('fill', d => colorScale(d.count));
// .style('fill', function(d) {return d.count})
let countInfo = data.map(function (testVariety) {
return testVariety.count;
})
countInfo.forEach(element => {
//add lable
marker.append('text')
.attr('x', padding + 7)
.attr('y', padding)
.attr('dy', '.31em')
.each(transform)
.text(function (d) {
return element
});
console.log(element)
});
function transform(d) {
d = new google.maps.LatLng(d.value.lat, d.value.lon);
d = projection.fromLatLngToDivPixel(d);
return d3.select(this)
.style('left', (d.x - padding) + 'px')
.style('top', (d.y - padding) + 'px')
}
};
};
// Bind overlay to the map
overlay.setMap(map);
});
console.log(element) 正在显示我想看到的结果,但我不知道如何在屏幕上显示它。
我觉得我快到了,但需要有人帮忙。
【问题讨论】:
-
我的代码是基于bl.ocks.org/mbostock/899711
标签: javascript arrays json google-maps d3.js