0

im trying to draw an arc in OpenLayers, in non Geo map it works well and here is my code:

function calculateArcPointsNonGeo(center, radius,startAngle,endAngle,segments)
 {
            var pointList =  [],
                Angle,
                x,
                y,
                point,
                i;

            var dAngle = segments + 1;
            for (i = 0; i < dAngle; i++) {
                Angle = startAngle - (startAngle - endAngle) * i / (dAngle - 1);
                 x = center.X + radius * Math.cos(Angle * Math.PI / 180);
                 y = center.Y + radius * Math.sin(Angle * Math.PI / 180);
                point = new MapPoint(x,y);
                pointList.push(point);
            }

            return pointList;
        }

in addition there is a great Example that demonstrates it , but when im trying to do this on Geo map (WGS84 coordinate system) its start to acting weird, i assume that i need to convert degrees to radinas or something like this. please help

1

solved It:

function calculateArcPointsGeo(center, radius, startAzimuth, endAzimuth, segments) {
        var pointList = [],
            point,
            i,
            d = radius / 1000, // d = distance in km
            R = 6371, // km, R = earth's radius (mean radius = 6,371km)
           lat1 = deg2Rad(center.Y),
           lon1 = deg2Rad(center.X),
           lat2,
           lon2,
           dAngle = segments + 1,
           azimuth;
        startAzimuth = deg2Rad(startAzimuth);
        endAzimuth = deg2Rad(endAzimuth);
        for (i = 0; i < dAngle; i++) {
            azimuth = startAzimuth - (startAzimuth - endAzimuth) * i / (dAngle - 1);
            lat2 = Math.asin(Math.sin(lat1) * Math.cos(d / R) +
                                  Math.cos(lat1) * Math.sin(d / R) * Math.cos(azimuth));

            lon2 = lon1 + Math.atan2(Math.sin(azimuth) * Math.sin(d / R) * Math.cos(lat1),
                                           Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2));
            lat2 = 180 * lat2 / Math.PI;
            lon2 = 180 * lon2 / Math.PI;

            point = new MapPoint(lon2, lat2);
            pointList.push(point);
        }

        return pointList;
    }

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.