Creating Circle

Last post 6/30/2009 9:42:49 PM by Chris Pietschmann. 4 replies. << Back to Web.Maps.VE v2.0 General
6/18/2009 3:28:15 PM
Arul

Creating Circle

Hi,

How can I create a circle (like polyline & polgon) using VE v2.4 ?

Thanks

6/19/2009 3:16:08 AM
Chris Pietschmann

Re:Creating Circle

Here's a link to a blog post I wrote a while back that demonstrates how to draw a circle on the map with a given radius.

http://pietschsoft.com/post/2008/02/Virtual-Earth-Draw-a-Circle-Radius-Around-a-LatLong-Point.aspx

The code and example in this post are in JavaScript, but it can be easily converted over to C# or VB.NET for use with the Web.Maps.VE control.

Let me know if you have any further questions.
6/21/2009 3:37:45 PM
cay187

Re:Creating Circle

you should convert it.  it would be very helpful!
6/22/2009 2:12:26 AM
cay187

Re:Creating Circle

i need to draw a circle when the aspx page loads.  i already have a lat and long and a radius of 500 feet or .0947 miles.  i want to draw the raduis when the page loads.

 

<link href="../../main.css" rel="stylesheet" type="text/css" />
            <script type="text/javascript" src="GeoCodeCalc.js"></script>
    <script type="text/javascript" src="CreateCircle.js"></script>
   
</head>
<body onload="LoadMap();">

 

<Simplovation:Map runat="server" ID="Map1" Width="800px" Height="500px"
        AsyncPostbackPassShapes="false" EnableBirdseye="false"
        Latitude="43.0889491834659"
        Longitude="-88.0128479003906"       
        Zoom="11"
        OnChangeView="Map1_ChangeView"
        OnMapLoaded="Map1_MapLoaded"
        />
   
    <br />

<script type="text/javascript">


    
        function LoadMap() {
            var map = $find("<%=Map1.ClientID%>");
            var units = GeoCodeCalc.EarthRadiusInMiles;
            var radius = parseFloat(".0947");
            var myLatLong = new Simplovation.Web.Maps.VE.LatLong(43.0889491834659, 88.0128479003906);
            var circle = CreateCircle(myLatLong, radius, units);
            circle.HideIcon();
            map.AddShape(circle);
        }               
    </script>

 

i am trying to combine the Dynamic search and the redius code into the same page.  i am mapping where sex offenders live and where schools and day cares are.  they cannot live within a 500 foot radius of a school or day care.  i have purchased your v1.0 map and will buy the v2.0 if i can get this working. 

 

6/30/2009 9:42:49 PM
Chris Pietschmann

Re:Creating Circle

cay187, You really don't want to manipulate the Map object directly from the Body OnLoad event on the client-side. The reason for this is because you can not guarantee that the Map as finished loading when the Body OnLoad event is fired. Instead you'll want to perform you first Map manipulations during the Map's own Loaded event which you can handle with JavaScript by setting the Map.OnClientMapLoaded property to the name of the JavaScript function you want to be called once the Map has finished loading.

Also, below is an example of the JavaScript code I linked to in a previous post translated to C#. Here's an example of how to calculate and draw circles from the server-side from within the Map.OnClick event.

Here's the code that handles the Map.OnClick event, and plots the Shape of Type Polyline on the Map:

protected void Map1_Click(object sender, Simplovation.Web.Maps.VE.AsyncMapEventArgs e)
{
    // Calculate out the points to draw a Circle around the clicked point.
    var circle = CreateCircle(e.latlong);

    Map1.AddShape(circle);
}

public Simplovation.Web.Maps.VE.Shape CreateCircle(Simplovation.Web.Maps.VE.LatLong centerPoint)
{
    // Create Geocode/Location Points to draw a circle with a Radius of 500 Miles
    var locs = GeoCodeCalc.CreateCircle(
        new GeoCodeCalc.Location() { Latitude = centerPoint.Latitude.Value, Longitude = centerPoint.Longitude.Value },
        500, GeoCodeCalc.EarthRadius.Miles
        );

    // Convert Locations to collection of LatLong objects to be used for plotting a Shape
    var shapePoints = new List<Simplovation.Web.Maps.VE.LatLong>();
    foreach (var loc in locs)
    {
        shapePoints.Add(new Simplovation.Web.Maps.VE.LatLong(loc.Latitude, loc.Longitude));
    }

    // Return a Shape of Type Polyline with the Circle Points calculated above
    return new Simplovation.Web.Maps.VE.Shape(
        Simplovation.Web.Maps.VE.ShapeType.Polyline,
        shapePoints
        );
}

Here's the GeoCodeCalc class used above:

using System;
using System.Collections.Generic;

/// <summary>
/// Summary description for GeoCodeCalc
/// </summary>
public static class GeoCodeCalc
{
    public enum EarthRadius : int
    {
        Miles = 3956,
        Kilometers = 6367
    }

    public class Location
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

    public static double ToRadian(double v)
    {
        return v * (Math.PI / 180);
    }

    public static double ToDegrees(double radians)
    {
        return radians * 180 / Math.PI;
    }

    public static double DiffRadian(double v1, double v2)
    {
        return ToRadian(v2) - ToRadian(v1);
    }

    public static double CalcDistance(Location location1, Location location2, EarthRadius radius)
    {
        return CalcDistance(location1.Latitude, location1.Longitude, location2.Latitude, location2.Longitude, radius);
    }

    public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, EarthRadius radius)
    {
        return ((double)radius) * 2 * Math.Asin(
            Math.Min(1,
                Math.Sqrt(
                    (
                        Math.Pow(Math.Sin((GeoCodeCalc.DiffRadian(lat1, lat2)) / 2.0), 2.0) +
                        Math.Cos(GeoCodeCalc.ToRadian(lat1)) * Math.Cos(GeoCodeCalc.ToRadian(lat2)) *
                        Math.Pow(Math.Sin((GeoCodeCalc.DiffRadian(lng1, lng2)) / 2.0), 2.0)
                    )
               )
           )
       );
    }

    public static List<Location> CreateCircle(double lat, double lng, double circleRadius, EarthRadius units)
    {
        return CreateCircle(new Location() { Latitude = lat, Longitude = lng }, circleRadius, units);
    }

    public static List<Location> CreateCircle(Location loc, double circleRadius, EarthRadius units)
    {
        var earthRadius = (double)units;
        var lat = GeoCodeCalc.ToRadian(loc.Latitude); //radians
        var lon = GeoCodeCalc.ToRadian(loc.Longitude); //radians
        var d = circleRadius / earthRadius;  // d = angular distance covered on earth's surface
        var locs = new List<Location>();
        for (var x = 0; x <= 360; x++)
        {
            var p2 = new Location();
            var brng = GeoCodeCalc.ToRadian(x); //radians
           
            var latRadians = Math.Asin(Math.Sin(lat) * Math.Cos(d) + Math.Cos(lat) * Math.Sin(d) * Math.Cos(brng));
            var lngRadians = lon + Math.Atan2(Math.Sin(brng) * Math.Sin(d) * Math.Cos(lat), Math.Cos(d) - Math.Sin(lat) * Math.Sin(latRadians));
           
            locs.Add(new Location() { Latitude = GeoCodeCalc.ToDegrees(latRadians), Longitude = GeoCodeCalc.ToDegrees(lngRadians) });
        }
       
        return locs;
    }
}