0

I have a position (lon, lat) and a radius. The radius is a float value and is in the km (kilometer) unit.

For example: Lat=46.92, Lon=7.95 and radius 5km.

Now my goal is to draw a circle or point at this position with this radius.

I'm using the follow transformation:

function GetLonLatObj(lat, lon) {

    var lonLat = new OpenLayers.LonLat( lon ,lat )
    .transform(
        new OpenLayers.Projection("EPSG:4326"), // Transformation aus dem Koordinatensystem WGS 1984
        map.getProjectionObject() // in das Koordinatensystem 'Spherical Mercator Projection'
    );

        return lonLat
}

To draw the circle on the right position is no problem. Unfortunately the circle is to small and has no radius of 5 km on the map.

Here is my code:

var map = new OpenLayers.Map("mapdiv");

map.addLayer(new OpenLayers.Layer.OSM('OpenStreetMap',
    "https://a.tile.openstreetmap.org/${z}/${x}/${y}.png",
    "https://b.tile.openstreetmap.org/${z}/${x}/${y}.png",
    "https://c.tile.openstreetmap.org/${z}/${x}/${y}.png"
));

map.addLayer(vectorNotamLayer);

zoom = 12;

var vectorNotamLayer = new OpenLayers.Layer.Vector("Simple Geometry", {

    styleMap: new OpenLayers.StyleMap({
        'default': {
        pointRadius: "${Radius}",
        label : "${Title}",
        fontSize: "20px",
        fontWeight: "bold",
        labelAlign: "cm",
        labelYOffset: -10,
        fillOpacity: 0.4,
        labelOutlineWidth: 3
        }
    }),

});

    var lon = 7.95;
    var lat = 46.92; 
    var radius = 5;
    var upper = item.upper;
    var lower = item.lower;
    var number = item.number;

    var title = series + number + " " + lower + "-" + upper;

    var latLonObj = GetLonLatObj(lat, lon);

    var point = new OpenLayers.Geometry.Point(latLonObj.lon, latLonObj.lat);

    var pointFeature = new OpenLayers.Feature.Vector(point);

    pointFeature.attributes = {
        Title: title,
        Radius: radius,
    };

    vectorNotamLayer.addFeatures([pointFeature]);

Can sombody help me here please?

Edit:

I changed the code from:

    var pointFeature = new OpenLayers.Feature.Vector(point);

    pointFeature.attributes = {
        Title: title,
        Radius: radius,
    };

To

var poly = OpenLayers.Geometry.Polygon.createRegularPolygon
(
        point,
        radius,
        36,
        0
);
Buhli
  • 33
  • 1
  • 5
  • The radius is specified in projection units which in the case of web mercator projection is the true distance on the ground only at the equator. At latitude 60 everything appears twice the size the same area would appear at the equator (compare Greenland and Africa) so a 5km radius would only cover 2.5km. You could calculate the true distance between points supposedly 1km apart at your latitude using `computeDistanceBetween` http://dev.openlayers.org/releases/OpenLayers-2.13.1/doc/apidocs/files/OpenLayers/Spherical-js.html and scale the radius accordingly. – Mike Jul 23 '20 at 13:01
  • A quick way would be to divide the radius by the cosine of the latitude. – Mike Jul 23 '20 at 13:05
  • Are you looking for something like this: http://www.geocodezip.com/OL_5.3.0_geocodeSanDiego_circlesExample.html – geocodezip Jul 23 '20 at 13:44
  • @Mike, Thank you very much for your feedback. According to your second comment you mean divide like this? radius/Math.cos(Lat*(Math.PI/180)) – Buhli Jul 23 '20 at 14:01
  • @geocodezip, Thank you very much for your feedback. Yes, can you tell me how you did this? With the radius, calculation and the zoom behavior? – Buhli Jul 23 '20 at 14:05
  • You can look at the source (view-source on that page). I answered your question with some details, didn't have a lot of time, let me know if you need further details. – geocodezip Jul 23 '20 at 15:06
  • Yes, the latitude must be converted to radians – Mike Jul 23 '20 at 16:23
  • @geocodezip, I changed my code a bit and now I'm using a polygon to create the circle. I checked also view source code and there I saw the function drawCircle. But still I'm confused. In my example I have a positon (Lon/Lat) and a radius of 5km (5000m). Can you tell me, what excalty I have to calc that I get a point/circle with a radius of 5000m on the map? Thank you very much – Buhli Jul 24 '20 at 11:14

0 Answers0