I am currently building a webapp using openlayers 3.

I build in "GPS-tracking" as a features and I got some questions with that. So, is it possible to run a function if a GPS-Position as a point-feature, reaches or "collides" with a polygon, which is drawn on the map?


if(positionFeature.getCoorinates() === polygon1.getCoordinates()){
   return "do someting";
} else { 
   return false;

1 Answer 1


I propose that you get the closest point of the polygon to your point, compute the distance and set the condition on that distance being smaller than a precision value ( which you define ) :

var wgs84sphere= new ol.Sphere(6378137);
var distance= wgs84Sphere.haversineDistance(yourPolygon.getGeometry().getClosestPoint(yourPoint.getGeometry().getCoordinates()),yourPoint.getGeometry().getCoordinates());
     //do something
     // something else
  • What Unit (Meters, Kilometers) is "yourValue"?
    – jokey
    Commented Sep 2, 2016 at 12:33
  • @jokey meters, you can always convert it to another unit Commented Sep 2, 2016 at 12:35
  • Thanks, how would this work for other projections (e.g CH1903+) without a sphere?
    – jokey
    Commented Sep 9, 2016 at 5:52
  • @jokey the only way I know to measure real distance with OL-3 is through the ol.Sphere , you can transform the projection of your geometries only when measuring them without applying that transformation on the renderning of the features and the map Commented Sep 9, 2016 at 9:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.