Ayenew Yihune Demeke
Ayenew Yihune Demeke

Reputation: 1277

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.

Upvotes: 3

Views: 737

Answers (1)

RoToRa
RoToRa

Reputation: 38431

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) {
     // ...
  }
});

Upvotes: 2

Related Questions