I want to give users the ability to find out how far they are from a Point of Interest to the edge of a radius on a map. I would also like to convert that unit to kilometers, meter or nautical mile.
I understand that all polygons are drawn in meters.
I am using fromCircle
to convert a circle to a geometer polygon. Please help me. I remember there was a getbound()
function in openlayers 2 but i can not find it anymore to use to calculate the distance form the the point of interest or center of the map to the edge. I have searched through stackoverflow for days but can not find exactly what is need or the solution that worked with the new version of openlayers.
var vectorSource = vectorLayer.getSource();
var centerLongitudeLatitude = map.getView().getCenter();
var viewProjection = map.getView().getProjection();
var pointResolution = olProj.getPointResolution(viewProjection, 1, centerLongitudeLatitude);
console.log('pointResolution', pointResolution)
function addCirclePolygon(coordinates, radius=1600) {
var _radius = radius/pointResolution;
// var circle = new Feature(new Circle(coordinates, _radius));
var circle = new Feature(new Circle(coordinates, _radius));
circle.setStyle(new Style({
stroke: new Stroke({
color: 'blue',
width: 3
}),
fill: new Fill({
color: 'rgba(0, 0, 255, 0.1)'
})
}));
var geom=circle.get('geometry');
if (circle instanceof Circle) {
circle.set('geometry', fromCircle(geom));
}
vectorSource.addFeature(circle);
}
The distance from a point to the edge of a ctrcle is the distance from the point to the center of the circle minus the radius.
But OpenLayers has a
getClosestPoint
method which will work with any geometry:Then you can calculate a distance using Pythagoras and adjust for point resolution: