2

In OpenLayers, we get the line feature on the clicked point by such kind of codes:

      map.on('singleclick', function(e) {
        var hit = false;
        map.forEachFeatureAtPixel(e.pixel, function() {
          hit = true;
        }, {
          hitTolerance: hitTolerance
        });
        if (hit) {
          style.getStroke().setColor('green');
          statusElement.innerHTML = ' A feature got hit!';
        } else {
          style.getStroke().setColor('black');
          statusElement.innerHTML = ' No feature got hit.';
        }
        feature.changed();
      });

But, with this, we can't get exact point coordinates on the line, because of hitTolerance setting's effect. How to get nearest point coordinates on the line?

3
  map.on('singleclick', function(e) {
    var hit = false;
    var point;
    map.forEachFeatureAtPixel(e.pixel, function(feature) {
      hit = true;
      point = feature.getGeometry().getClosestPoint(e.coordinate);
    }, {
      hitTolerance: hitTolerance
    });

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.