turf.point
Takes coordinates and properties (optional) and returns a new Point feature.
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<link rel="stylesheet" href="assets/css/styles.css" type="text/css">
<script src="//cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
<script src="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" type="text/javascript"></script>
<title>Turf and OpenLayers 3 - Helper point</title>
</head>
<body>
<div id="map" class="map"></div>
<script type="text/javascript">
// Tower of Bretagne without properties declared
var pt1 = turf.point([-1.5579479, 47.2175545]);
// Cranes of Nantes with properties defined
var pt2 = turf.point([-1.5741021, 47.1991929], {
name: 'Grue grise',
man_made: 'crane'
});
var pt3 = turf.point([-1.57007, 47.20507], {
name: 'Grue jaune',
man_made: 'crane'
});
// Declare a formatter to read GeoJSON
var format = new ol.format.GeoJSON();
// Declare a source
var vectorSource = new ol.source.Vector();
// When reading feature, reproject to EPSG 3857
var feature = format.readFeature(pt1, {
featureProjection: 'EPSG:3857'
});
// Add a feature
vectorSource.addFeature(feature);
// Create an Array of ol.Feature
var feature_pt2 = format.readFeatures(pt2, {
featureProjection: 'EPSG:3857'
});
// Create another Array of ol.Feature
var feature_pt3 = format.readFeatures(pt3, {
featureProjection: 'EPSG:3857'
});
// Combine together two Array of ol.Feature, just to play and learn
var pt2_pt3 = feature_pt2.concat(feature_pt3);
// Add features to the source
vectorSource.addFeatures(pt2_pt3);
// Declare a vector layer with the already
// created source containing added features
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: [
new ol.style.Style({
image: new ol.style.Circle({
stroke: new ol.style.Stroke({
color: 'white'
}),
fill: new ol.style.Fill({
color: '#1f6b75'
}),
radius: 5
})
})
]
});
// Instanciate a map and add layers
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
view: new ol.View({
center: ol.proj.transform(
[-1.563046, 47.214751],
'EPSG:4326',
'EPSG:3857'
),
zoom: 13
})
});
</script>
</body>
</html>