Hey I’ve tried putting my markers for several hours on my map and i cant see the problem, cause im a newbie.
I started everything with a code that i’ve found online:
let results =[[1, 'SCOTT', 50.063509, 19.932827, 2222,'y'],[2, 'Marin', 50.065070, 19.931180, 2222, 'n']];
var points={};
points['type']='Feature Collection';
points['features']=[];
Than i put a code, that was working perfectly:
var points = [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [results[1][3], results[1][2]]
}
},
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [results[0][3], results[0][2]]
}
}
];
But i wanted to make it more automatic, so if more results (bikes) appear i wouldn’t have to write everything down. So i came up with a loop idea to make as many points (where are my bikes located) as they are in the array. Ive checked everything in the loop, it works perfectly, even coordinates are beeing shown when i console log them, but i have a big problem with a points['features'].push(newFeature); , i think this is my main issue and I cant fix it now, so im asking you for help.
This is my for loop , that i’ve made:
for (var k in results){
var newFeature = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [results[k][3], results[k][2]]
}
}
points['features'].push(newFeature);
}
Do you know how can i make it the same as 1st array?
I can put also code, that i took from my map privider that shows the points on the map:
map.on('load', () => {
map.addSource('places', {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": points
}
});
// Add a layer showing the places.
map.addLayer({
'id': 'places',
'type': 'circle',
'source': 'places',
'paint': {
'circle-color': '#4264fb',
'circle-radius': 6,
'circle-stroke-width': 2,
'circle-stroke-color': '#ffffff'
}
});
// Create a popup, but don't add it to the map yet.
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on('mouseenter', 'places', (e) => {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
// Copy coordinates array.
const coordinates = e.features[0].geometry.coordinates.slice();
const description = e.features[0].properties.description;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates).setHTML(description).addTo(map);
});
map.on('mouseleave', 'places', () => {
map.getCanvas().style.cursor = '';
popup.remove();
});
});