0

I managed to add markers as Feature objects in a Vector layer. I can simply move the markers to its new locations by using Feature.setGeometry() method.

Next thing is to change the marker's direction or bearing, as you will. The direction is calculated based on previous location and new location of the marker. How can I rotate the marker? I can't find the right way to do it. OpenLayers documentation is a bit overwhelming to me and I can't find an example also.

This is in short the code I have right now:

// create a marker
let marker = new ol.Feature({
    geometry: new ol.geom.Point(ol.proj.fromLonLat([4.333959, 52.060110]))
});

// add it to the markersLayer
markersLayer.getSource().addFeature(marker);

// set marker to a new position
marker.setGeometry(new ol.geom.Point(ol.proj.fromLonLat(loc)));

// rotate marker to x degrees
// but don't know how to do it :(
marker.???

So how can I rotate the marker? What's the best practice solution? The marker has a small arrow image always pointing to the north.

2
  • Point cannot be rotated. Object/geometry must have some dimension/s for it to be rotated.
    – TomazicM
    Commented Jun 14, 2022 at 10:55
  • Can you provide me a best practice and working example? I'm a new kid on the block here, so I'm not well know converning the map design philosophy. Commented Jun 14, 2022 at 11:20

1 Answer 1

1

its pretty simple you can use styles to set the rotation. I used it in following manner setting styles of icon with rotation property and pass bearing info into it.

let styles = [
                new Style({
                  image:
                    new Icon({
                      anchor: [0.5, 24],
                      anchorXUnits: 'fraction',
                      anchorYUnits: 'pixels',
                      src: `assets/images/SVG/truck-moving-solid.svg`,
                      scale: 0.65,
                      rotation: feature.bearing
                    }),
                }),
              ]
3
  • Thnx! But how can I access the Feature object that has its coordination changed? Sorry for the stupid questions, but I try to understand the concept of OpenLayers, it's quite different from Apple's map on iOS framework. Commented Jun 14, 2022 at 11:57
  • what i am doing is that on every GPS pings data i create a new feature with new coordinate and set feature with new values like below and set it in the layer and finally layer to map. i have a method which do this layer create and set i just pass the feature return new Feature({ geometry: new Point(fromLonLat([element.lon, element.lat])), direction: element.direction, rno: element.routeNumber, utcTime: element.utcTime, }) Commented Jun 14, 2022 at 12:09
  • Ah I see, the bearing info you get it from the GPS data. Commented Jun 14, 2022 at 12:18

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.