I'm trying to draw rects on a canvas for each element in my array. For the positioning I use the longitude and latitude values of the elements.
My array looks something like this and contains 50.000 elements that are objects:
var modified_array =
[{"city":"NYC","longitude":-73.935242,"latitude":40.730610},
{"city":"NYC","longitude":-74.044502,"latitude":40.689247}]
EDIT: The solution from @le_m helped me out a lot and I implemented a filter like he suggested:
const test2 = test.filter(({latitude, longitude}) => latitude != null
&& longitude != null);
function getBoundingRect(test2) {
let left = Infinity, right = -Infinity;
let top = Infinity, bottom = -Infinity;
for (let {latitude, longitude} of test2) {
if (left > latitude ) left = latitude;
if (top > longitude) top = longitude;
if (right < latitude) right = latitude;
if (bottom < longitude) bottom = longitude;
}
return {x: left, y: top, width: right - left, height: bottom -
top};
}
function draw(ctx, test2) {
let boundingRect = getBoundingRect(test2);
let scale = Math.min(canvas.width, canvas.height);
for (let {latitude, longitude} of test2) {
let x = (latitude - boundingRect.x) / boundingRect.width *
scale;
let y = (longitude - boundingRect.y) / boundingRect.height *
scale;
ctx.fillStyle = "rgba(65,105,225,0.2)";
ctx.fillRect(x - 5, y - 5, 4, 4);
}
}
draw(ctx, test2);
The filter doesn't seem to work What am I doing wrong?