Truextacy
Truextacy

Reputation: 562

OpenLayers 3 - getClosesPoint() of a given feature

I viewed this documentation and am trying to get the closest feature to a given feature like this:

var foo = pretendLocation.getGeometry();
console.log(getClosestPoint(foo));

I also tried:

console.log(pretendLocation.getClosestPoint());

pretendLocation is a feature I created like this,

var pretendLocation = new ol.Feature({
            geometry: new ol.geom.Point(ol.proj.transform([-121, 37], 'EPSG:4326', 'EPSG:3857'))
        });

Am I using getClosestPoint() incorrectly? What function do I use to get the closest feature to a given feature? I searched other posts but can not find a solution. I am getting console errors from both ways - getClosestPoint is not defined, as well as a cannot read property of 0 error.

Upvotes: 0

Views: 2811

Answers (1)

Sumanth Shastry
Sumanth Shastry

Reputation: 1159

If you see the documentation getClosestPoint() is an api of ol.geom.Point.

So it needs to be called with geom object reference and ol.Coordinate to be passed to the method. getClosestPoint() will find the closest point in the geometry by evaluating coordinate passed.

var foo = pretendLocation.getGeometry();
console.log(foo.getClosestPoint([-121, 37]));

foo is the feature's geometry . [-121,37] is the ol.Coordinate for which closest point is searched.

Upvotes: 2

Related Questions