1

我已经能够使用给定中心点周围的纬度和经度值在我的地图上绘制一个椭圆。虽然我在地图上看到了一个形状,但我得到的是椭圆而不是圆形,我认为它与指定的距离不匹配。我打算用它来显示该圆圈内的对象(这将在稍后我可以正确显示圆圈后完成,这就是为什么我需要一个圆圈而不是椭圆,因为它应该是完美的圆形)。

我正在使用 Bing 地图 API。我希望从通过参数传入的中心以给定的英里(距离)绘制圆,参数中称为英里的另一个变量只是保持 1D 的双精度值。我认为问题与我的数学计算方式有关。有没有人知道如何改进此代码以更好地计算我的里程。

private void drawPoly(SearchLocation center, Double miles)
{
//amount of vertex
double vertexCount = 100D;
         //used by the api to carried out searches
List<SearchLocation> vertices = new List<SearchLocation>();
double v = 0;
double radians = Math.PI / 180D;
double radiansPerDegree = Math.PI / 180D;
double degreePerVertex = 360D / vertexCount;
double radiansPerVertex = degreePerVertex * radiansPerDegree;
var centerOfMap = center;
const double degLatMiles = 68.68637156368D;
double degLonMiles = Math.Cos(center.Latitude.Value) * (68.68637156368D);
double milesLat = (miles * degLatMiles) / 3600;
double milesLon = (miles * degLonMiles) / 3600;
for (v = 0; v < vertexCount; v++)
{
    radians = v * radiansPerVertex;
               //adds the miles from the center point and draws a circle
    double centrLat = center.Latitude.Value + (milesLat * Math.Sin(radians));
    double centrLon = center.Longitude.Value + (milesLon * Math.Cos(radians));
    vertices.Add(new SearchLocation() { Latitude = centrLat, Longitude = centrLon });
}
4

2 回答 2

1

好的,我误解了你的问题。这应该工作:

    /// <summary>
    /// Calculates the end-point from a given source at a given range (meters) and bearing (degrees).
    /// This methods uses simple geometry equations to calculate the end-point.
    /// </summary>
    /// <param name="source">Point of origin</param>
    /// <param name="range">Range in meters</param>
    /// <param name="bearing">Bearing in degrees</param>
    /// <returns>End-point from the source given the desired range and bearing.</returns>
    public static PointLatLng CalculateDerivedPosition(PointLatLng source, double range, double bearing)
    {
        double latA = source.Lat * DEGREES_TO_RADIANS;
        double lonA = source.Lng * DEGREES_TO_RADIANS;
        double angularDistance = range / EARTH_RADIUS_M;
        double trueCourse = bearing * DEGREES_TO_RADIANS;

        double lat = Math.Asin(
            Math.Sin(latA) * Math.Cos(angularDistance) +
            Math.Cos(latA) * Math.Sin(angularDistance) * Math.Cos(trueCourse));

        double dlon = Math.Atan2(
            Math.Sin(trueCourse) * Math.Sin(angularDistance) * Math.Cos(latA),
            Math.Cos(angularDistance) - Math.Sin(latA) * Math.Sin(lat));

        double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;

        return new PointLatLng(
            lat / DEGREES_TO_RADIANS,
            lon / DEGREES_TO_RADIANS);
    }

Juste 以您的中心为来源:

for (int i = 0; i < 360; i++)
{
  vertices.Add(CalculateDerivedPosition(center, circleRadius, i));
}
于 2012-10-15T09:24:01.703 回答
0

为了防止在某些纬度上出现省略号,我使用以下代码:

// Function to draw circle on map:

private void DrawCircle(BasicGeoposition CenterPosition, int Radius)
    {
    Color FillColor = Colors.Purple;
    Color StrokeColor = Colors.Red;
    FillColor.A = 80;
    StrokeColor.A = 80;
    Circle = new MapPolygon
        {
            StrokeThickness = 2,
            FillColor = FillColor,
            StrokeColor = StrokeColor,
            Path = new Geopath(Functions.CalculateCircle(CenterPosition, Radius))
        };
    mpBingMaps.MapElements.Add(Circle);
}

// Constants and helper functions:

const double earthRadius = 6371000D;
const double Circumference = 2D * Math.PI * earthRadius;

public static List<BasicGeoposition> CalculateCircle(BasicGeoposition Position, double Radius)
{
    List<BasicGeoposition> GeoPositions = new List<BasicGeoposition>();
    for (int i = 0; i <= 360; i++)
    {
        double Bearing = ToRad(i);
        double CircumferenceLatitudeCorrected = 2D * Math.PI * Math.Cos(ToRad(Position.Latitude)) * earthRadius;
        double lat1 = Circumference / 360D * Position.Latitude;
        double lon1 = CircumferenceLatitudeCorrected / 360D * Position.Longitude;
        double lat2 = lat1 + Math.Sin(Bearing) * Radius;
        double lon2 = lon1 + Math.Cos(Bearing) * Radius;
        BasicGeoposition NewBasicPosition = new BasicGeoposition();
        NewBasicPosition.Latitude = lat2 / (Circumference / 360D);
        NewBasicPosition.Longitude = lon2 / (CircumferenceLatitudeCorrected / 360D);
        GeoPositions.Add(NewBasicPosition);
    }
    return GeoPositions;
}

private static double ToRad(double degrees)
{
    return degrees * (Math.PI / 180D);
}

此代码适用于小于几英里的小半径。

于 2014-12-31T09:29:42.197 回答