2

I have the following map and vector layer declaration that is working:

var map;
var vectorLayer = new ol.layer.Vector({
 target: "points",
 source: new ol.source.Vector(),
 style: new ol.style.Style({
 image: new ol.style.Icon({
 anchor: [0.5, 0.5],
 anchorXUnits: "fraction",
 anchorYUnits: "fraction",
 src: "RedDot.svg",
 }),
 label: "My Junk", 
 text: new ol.style.Text({
 font: '15px Calibri,sans-serif',
 fill: new ol.style.Fill({ color: '#000' }),
 stroke: new ol.style.Stroke({
 color: '#fff', width: 2
 }),
 text: "My Truck",
 offsetY: 18,
 })
 })
 });

(key info - Note the default "My Truck" label for each point)

I have the following function that adds a point to the map (I don't honestly know if it's creating a new layer for each point, or adding all the points to the default layer):

function add_map_point(lat, lng, txt) {
 var source = vectorLayer.getSource();
 var pointFeature = new ol.Feature({
 geometry: new ol.geom.Point(ol.proj.transform([parseFloat(lng), parseFloat(lat)], 'EPSG:4326', 'EPSG:3857')),
 });
 source.addFeature(pointFeature);
 }

This all works - no issues.

What I would like to do is use the TXT variable passed to that function to update the TEXT label ("My Truck" by default). It seems like I should be able to do something simple like source.getStyle().setStyle(getText().setText(text: txt)); (over simplified I assume).

Could someone point out how this could be done?

I've been searching all morning, and all I can find is redeclaring the style again, which seems unnecessary.

menes
1,4272 gold badges8 silver badges25 bronze badges
asked May 12, 2021 at 16:38

1 Answer 1

5

First define a style, with text styling options but no text

var style = new ol.style.Style({
 image: new ol.style.Icon({
 anchor: [0.5, 0.5],
 anchorXUnits: "fraction",
 anchorYUnits: "fraction",
 src: "RedDot.svg",
 }),
 text: new ol.style.Text({
 font: '15px Calibri,sans-serif',
 fill: new ol.style.Fill({ color: '#000' }),
 stroke: new ol.style.Stroke({
 color: '#fff', width: 2
 }),
 offsetY: 18,
 })
});

Then give your layer a style function which sets the style text from a property of a feature

var vectorLayer = new ol.layer.Vector({
 target: "points",
 source: new ol.source.Vector(),
 style: function (feature) {
 style.getText().setText(feature.get('text'));
 return style;
 }
});

When you create features include a text property

function add_map_point(lat, lng, txt) {
 var source = vectorLayer.getSource();
 var pointFeature = new ol.Feature({
 geometry: new ol.geom.Point(ol.proj.transform([parseFloat(lng), parseFloat(lat)], 'EPSG:4326', 'EPSG:3857')),
 text: txt
 });
 source.addFeature(pointFeature);
}
answered May 12, 2021 at 17:08
3
  • it works! But I don't understand how it's different - it really just looks like you pulled out the style definition and defined it before assigning it, but without the TEXT declaration. Unless the getText() function is actually not a static definition when the style is declared, but is a dynamic definition that updates itself when the TEXT feature is added later in the add_map_point function. Do I have that last part right? Commented May 12, 2021 at 19:23
  • The important part is setText() in the style function. The function is called whenever a feature is rendered, and setText() ensures the style returned contains the correct text for that feature. Commented May 13, 2021 at 9:57
  • Mike, thanks for the clarification - I had no idea (mind blown actually) that you could assign a function as a "value" for a variable in Javascript. Very cool, and weirdly unsettling at the same time. Commented May 14, 2021 at 12:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.