Create addLayer LINE from coordinates

I am trying to create a line between two coordinates,
But I can’t find an example with lines, simple like this:

map.addLayer({
		'id': '123',
		'type': 'line',
		'source': {
			'type': 'geojson',
			'data': {
				'type': 'Feature',
				'geometry': {
					'type': 'line',
					'coordinates': [[[2.7505287, 41.6709659],[2.5010908655347097, 41.57083364442753]]]
				}
			}
		},
		'layout': {
		  'line-cap': 'round',
		  'line-join': 'round'
		},
		'paint': {
			'line-color': '#ff0000',
			'line-width': 9
		}
	});

Hi Juan.

Welcome to our community!

I adjusted your GeoJson string a little bit and it works now:

map.addLayer({
        'id': '123',
        'type': 'line',
        'source': {
            'type': 'geojson',
            'data': {
                'type': 'FeatureCollection',
                'features': [
                    {
                        'type': 'Feature',
                        'geometry': {
                            'type': 'LineString',
                            'properties': {},
                            'coordinates': [
                                [2.7505287, 41.6709659],
                                [2.5010908655347097, 41.57083364442753]
                            ]
                        }
                    }
                ]
            }
        },
        'layout': {
            'line-cap': 'round',
            'line-join': 'round'
        },
        'paint': {
            'line-color': '#ff0000',
            'line-width': 9
        }
});

Let me know if you’ll have more questions.

Regards,
Mateusz

1 Like

thanks, work perfectly :slight_smile:

2 Likes