1

I have a simple OL-based map with the default EPSG:3857 projection and an interaction which lets me draw a LineString on the map. Then, I have an event listener registered on the VectorSource, which listens to addfeature events i.e. when the interaction ends by double-click, the addfeature event is fired. Here, in the addfeature callback, I want to transform the geometry of the LineString to EPSG:4326 and send the transformed coordinates to an external API which only accepts coordinates with this projection.

However, each time I attempt to transform the coordinates, the drawn LineString disappears from the map. Here's the problematic part in the code:

source.on('addfeature', function (evt) {
    const {feature} = evt;
    const featureGeometry = feature.getGeometry();
    addFeatureCallback(featureGeometry);
});

function addFeatureCallback(geometry) {
    geometry.transform('EPSG:3857', 'EPSG:4326'); // <-- this removes my drawn feature from the map. If I remove this line, the drawn LineString remains.
    const coords = geometry.getCoordinates();
    ...
}

I tried creating a new variable that references the geometry param from addFeatureCallback and calling the transform method unto it, but the problem still remains and the drawn line disappears. Is there a way to transform the feature geometry to another projection without it removing the LineString from the map? As a workaround, I could set the default projection in OL to be EPSG:4326, however, this breaks other parts of my project and I would really like to avoid doing that.

1 Answer 1

1

I found the solution. The problem was that I was modifying the geometry in place. Instead of that, now I first clone() then do the transform.

Answer here helped - https://stackoverflow.com/questions/36134974/transforming-coordinates-of-feature-in-openlayers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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