Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a link on Openlayers map

I have located a point on a map

var layer = new ol.layer.Vector({
    source: new ol.source.Vector({
        features: [
            new ol.Feature({
                geometry: new ol.geom.Point(ol.proj.fromLonLat(cord)),
                label: 'Label'
            })
        ]
    }),
    style: (feature) => {
            myStyle.getText().setText(feature.get('label'));
            return [myStyle];
        }
});

I just wanted to make a link to the label (or the point) that takes me to another page. Any help.

like image 963
Ayenew Yihune Avatar asked Jul 23 '26 01:07

Ayenew Yihune


1 Answers

Put a click event handler on your map and use getFeaturesAtPixel to find the feature (if any) that has been clicked on. For example:

map.on("click", (event) => {
  const features = map.getFeaturesAtPixel(event.pixel);
  if (features && features.length > 0) {
     // ...
  }
});
like image 156
RoToRa Avatar answered Jul 25 '26 14:07

RoToRa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!