I'm wondering if anyone can help me to understand if there is a way to convert an existing feature's geometry type? The reason for this is outlined in a previous question but basically when I drop a gpx file onto the map I need to convert certain 'routes' to shapes.

The API indicates that there is a setGeometry() function but I can't get it to work and there are no examples to draw from. Below are the methods I've tried so far.

feature.setGeometry(geometry, ol.geom.Polygon);
feature.values_.geometry = ol.geom.Polygon;

This might be a kludge but the only other alternative as far as I can tell is to parse every gpx element manually which will involve essentially replicating all the existing code in order to catch this error.

  • 1
    setGeometry is used like this: feature.setGeometry(new ol.geom.Point([1,2])); – ca0v Aug 11 '16 at 11:13
  • @ca0v thanks so much, I didn't realise the new constructor or coordinates were necessary - this now works like a charm! I don't know if you want to post your comment as an answer and I can accept it? Thanks again ;) – dvmac01 Aug 11 '16 at 12:02

setGeometry is used like this:

feature.setGeometry(new ol.geom.Point([1,2]));
| improve this answer | |
  • As I said in my previous comment, this works perfectly now. Thanks again @ca0v – dvmac01 Aug 11 '16 at 15:25

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.