0

I don't understand why my code doesn't work. If the map is displayed, the line is never drawn:

<script>
var long_deb = -1.28627;
var lat_deb = 43.49829;
var long_fin = -1.29394;
var lat_fin = 43.49858;
var attribution = new ol.control.Attribution({
  collapsible: false
});

var map = new ol.Map({
  controls: ol.control.defaults({ attribution: false }).extend([attribution]),
  target: 'map',
  view: new ol.View({
    center: ol.proj.fromLonLat([long_deb, lat_deb]),
    zoom: 15
  })
});
var layer = new ol.layer.Tile({
  source: new ol.source.OSM()
});
map.addLayer(layer);
var linestring = new ol.geom.LineString([[long_deb, lat_deb], [long_fin, lat_fin]], 'XY');
var lineString_feature = new ol.Feature ( {
    geometry: linestring
});
var layerLine = new ol.layer.Vector({
  source: new ol.source.Vector( {
    features: [ lineString_feature]
  })
});
map.addLayer(layerLine);
console.log(
    "lineString coordinates", linestring.getCoordinates(), 
    "extent", linestring.getExtent(), 
    "layout", linestring.getLayout());

var longueur =linestring.getLength();
console.log("longueur", longueur);
</script>

and the console message (log) :

lineString coordinates Array [ Array[2], Array[2] ] extent Array [ -1.29394, 43.49829, -1.28627, 43.49858 ] layout XY
longueur 0.007675480440988747

Any idea?

1
  • In the console check whether the layerline has coordinates.
    – neogeomat
    Apr 15, 2018 at 3:12

1 Answer 1

1

I've found the reason : it's a projection's problem. To resolve it :

function transform_geometry(element) {
  var current_projection = new ol.proj.Projection({code: "EPSG:4326"});
  var new_projection = layer.getSource().getProjection();

  element.getGeometry().transform(current_projection, new_projection);
}
var features = [lineString_feature];
var layerLine = new ol.layer.Vector({
  source: new ol.source.Vector( {
    features: features
  })
});
features.forEach(transform_geometry);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.