locked
Draw circle around specified miles using ajax sdk v7.0 RRS feed

  • Question

  • Hi

    We want to draw circle around the specified radius. Suppose we want to draw circle 5 miles from my current location or any location specify by us.

    Any sample code available for this.

    Thanks!

    Balasaheb 

     

    Sunday, March 13, 2016 8:14 AM

Answers

  • This has been answered several times in the forums: https://social.msdn.microsoft.com/Forums/en-US/home?forum=bingmapsajax&sort=relevancedesc&brandIgnore=True&searchTerm=circle

    There are two ways to do this. The first is to doe the calculations to approximate the shape of a circle like this:

    function drawCircle(radius, origin) {
        var RadPerDeg = Math.PI / 180;
        var earthRadius = 3959;  
        var lat = origin.latitude * RadPerDeg;
        var lon = origin.longitude * RadPerDeg;
        var locs = new Array();
        var AngDist = parseFloat(radius) / earthRadius;
        for (x = 0; x <= 360; x++) { //making a 360-sided polygon
            var pLatitude, pLongitude;
            // With a nice, flat earth we could just say p2.Longitude = lon * sin(brng) and p2.Latitude = lat * cos(brng)
            // But it ain't, so we can't.  See http://www.movable-type.co.uk/scripts/latlong.html
            brng = x * RadPerDeg;
            pLatitude = Math.asin(Math.sin(lat) * Math.cos(AngDist) + Math.cos(lat) * Math.sin(AngDist) * Math.cos(brng)); //still in radians
            pLongitude = lon + Math.atan2(Math.sin(brng) * Math.sin(AngDist) * Math.cos(lat), Math.cos(AngDist) - Math.sin(lat) * Math.sin(pLatitude));
            pLatitude = pLatitude / RadPerDeg;
            pLongitude = pLongitude / RadPerDeg;
            locs.push(new Microsoft.Maps.Location(pLatitude, pLongitude));
        };
        circle = new Microsoft.Maps.Polygon(locs, { visible: true, strokeThickness: 2, strokeDashArray: "1", strokeColor: new MM.Color(200, 0, 0, 200, 0) });
        map.entities.push(circle);
    };

    The second method is to use the circle module that is available here: http://bingmapsv7modules.codeplex.com/wikipage?title=Circle%20Module


    http://rbrundritt.wordpress.com

    Monday, March 14, 2016 12:09 AM

All replies

  • This has been answered several times in the forums: https://social.msdn.microsoft.com/Forums/en-US/home?forum=bingmapsajax&sort=relevancedesc&brandIgnore=True&searchTerm=circle

    There are two ways to do this. The first is to doe the calculations to approximate the shape of a circle like this:

    function drawCircle(radius, origin) {
        var RadPerDeg = Math.PI / 180;
        var earthRadius = 3959;  
        var lat = origin.latitude * RadPerDeg;
        var lon = origin.longitude * RadPerDeg;
        var locs = new Array();
        var AngDist = parseFloat(radius) / earthRadius;
        for (x = 0; x <= 360; x++) { //making a 360-sided polygon
            var pLatitude, pLongitude;
            // With a nice, flat earth we could just say p2.Longitude = lon * sin(brng) and p2.Latitude = lat * cos(brng)
            // But it ain't, so we can't.  See http://www.movable-type.co.uk/scripts/latlong.html
            brng = x * RadPerDeg;
            pLatitude = Math.asin(Math.sin(lat) * Math.cos(AngDist) + Math.cos(lat) * Math.sin(AngDist) * Math.cos(brng)); //still in radians
            pLongitude = lon + Math.atan2(Math.sin(brng) * Math.sin(AngDist) * Math.cos(lat), Math.cos(AngDist) - Math.sin(lat) * Math.sin(pLatitude));
            pLatitude = pLatitude / RadPerDeg;
            pLongitude = pLongitude / RadPerDeg;
            locs.push(new Microsoft.Maps.Location(pLatitude, pLongitude));
        };
        circle = new Microsoft.Maps.Polygon(locs, { visible: true, strokeThickness: 2, strokeDashArray: "1", strokeColor: new MM.Color(200, 0, 0, 200, 0) });
        map.entities.push(circle);
    };

    The second method is to use the circle module that is available here: http://bingmapsv7modules.codeplex.com/wikipage?title=Circle%20Module


    http://rbrundritt.wordpress.com

    Monday, March 14, 2016 12:09 AM
  • Hi Ricky,

    Thanks for your reply. We got this.  

    Thanks!

    Balasaheb

    Monday, March 14, 2016 10:21 AM