0

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?

E.g

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

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());
if(distance<=yourValue){
     //do something
}
else{
     // something else
}
  • What Unit (Meters, Kilometers) is "yourValue"? – jokey Sep 2 '16 at 12:33
  • @jokey meters, you can always convert it to another unit – Hicham Zouarhi Sep 2 '16 at 12:35
  • Thanks, how would this work for other projections (e.g CH1903+) without a sphere? – jokey Sep 9 '16 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 – Hicham Zouarhi Sep 9 '16 at 9:21

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.