Drawing / Editing and Saving Polygons to Sql Server using Ajax v7

Answered Drawing / Editing and Saving Polygons to Sql Server using Ajax v7

  • Thursday, December 16, 2010 2:00 PM
     
     

    Hi, has anyone tried something like either of these samples with v7 yet and save it back to sql server?

    Drawing a polygon from scratch:

    http://www.beginningspatial.com/marking_out_features_using_virtual_earth

    Editing a polygon:

    http://www.garzilla.net/vemaps/PolygonDragHandle.aspx

     


    Jonathon

All Replies

  • Tuesday, January 18, 2011 4:47 PM
     
     

    Hello Jonathon

    I do not think there is any out of the box functionality from microsoft to save your Bing polygons to SQL server. Sometime ago (when its Bing 6.0) I worked on a project where I build a C# data access engine myself which basically converts bing VEShapes to equivalent SQL Server 2008 geometries and vice versa.

    Its also worth looking into http://www.esri.com/software/mapit/index.html if you are planning on spending some dollars.

    Hope this helps.


    Aditya Tadakaluru MCPD, MCITP, MCTS(Bing Maps)
  • Tuesday, January 18, 2011 9:05 PM
     
     

    Today is the last day before I'm just going to have to jump in and build it myself.

    I think I'm going to start with a draggable bounding box and see if that meets my needs.

    Does anyone know how I can attach events to Polygon Locations (Corners?)

  • Tuesday, January 18, 2011 9:27 PM
    Moderator
     
     

    I'll be updating the page at beginningspatial soon - just haven't got round to it yet. If you do start yours, be sure to share how you get on.

    I don't think you can attach events specifically to polygon "corners" - you'll have to attach events to pushpins placed at the same location, make those draggable, and then redefine the polygon location array to match the locations of those pushpins.


    Beginning Spatial with SQL Server http://www.apress.com/book/view/1430218290
  • Wednesday, January 19, 2011 12:02 PM
     
     Answered Has Code
    I ended up opting for a really crude solution that will satisfy my requirements for maybe another month.

    My polygon is actually a square, and I have a bounding box defined for it.

    Then I have a text box with a value that users can increment / decrement by clicking buttons. It would be really cool change this to a slider when I do a second pass on my app.

    Then I have the following defined (forgive the quality of the code, I haven't cleaned it up yet and I haven't included 2 function here. One that gets the initial result from an ajax call and one that initializes the map. Hence this code won't run for you cut & past.

    To save the polygon back to sql server  I used WKT (Well Know Text) and perform a SqlGeography.Parse({WKT})
    <script type="text/javascript"> 
      var MM = Microsoft.Maps;
      var currentResult;
      var map = null;
      var polygonPoints;
      var polygon = new MM.Polygon();
    
      function renderPolygon() 
      {
        map.entities.clear();
    
        polygon = new Microsoft.Maps.Polygon(polygonPoints, {
            strokeThickness: 2,
            strokeColor: new Microsoft.Maps.Color(156, 0, 255, 0),
            fillColor: new Microsoft.Maps.Color(156, 0, 0, 255)
          });
    
        map.entities.push(polygon);
    
        var boundingCorners = Microsoft.Maps.LocationRect.fromLocations(polygonPoints.slice(1));
    
        map.setView({ bounds: boundingCorners });
      } 
    
    function IncreasePolygon() 
    {
      var intSize = parseInt($("#PolygonResize").val());
      $("#PolygonResize").val(intSize + 1)
      RedrawBoundingBox();
    }
    
    function DecreasePolygon() {
      var intSize = parseInt($("#PolygonResize").val());
      $("#PolygonResize").val(intSize - 1)
      RedrawBoundingBox();
    }
    
    function RedrawBoundingBox() 
    {
      var newSize = $("#PolygonResize").val();
      var latResize = parseFloat(0.015) * parseFloat(newSize);
      var longResize = parseFloat(0.01) * parseFloat(newSize);
      var originalBox = currentResult.Box;
      var bb = new Object();
      bb.NorthLatitude = parseFloat(originalBox.NorthLatitude) + latResize;
      bb.SouthLatitude = parseFloat(originalBox.SouthLatitude) - latResize;
      bb.EastLongitude = parseFloat(originalBox.EastLongitude) + longResize;
      bb.WestLongitude = parseFloatoriginalBox.WestLongitude) - longResize;
      
      polygonPoints = GetPolygonPointsFromBoundingBox(bb);
      renderPolygon();
      $('#WKT').val(BuildWKTFromBoundingBox(bb));
    }
    
    function GetPolygonPointsFromBoundingBox(boundingBox) 
    {
      var bb = boundingBox;
      
      polygonPoints = [new MM.Location(bb.SouthLatitude, bb.WestLongitude),
                  new MM.Location(bb.SouthLatitude, bb.EastLongitude),
                  new MM.Location(bb.NorthLatitude, bb.EastLongitude),
                  new MM.Location(bb.NorthLatitude, bb.WestLongitude),
                  new MM.Location(bb.SouthLatitude, bb.WestLongitude)];
    
      return polygonPoints;
    }
    
    
    function BuildWKTFromBoundingBox(boundingbox) {
      var bb = boundingbox;
      var polygonWKT = 'POLYGON((' + bb.WestLongitude + ' ' + bb.SouthLatitude + ', ' +
                    bb.EastLongitude + ' ' + bb.SouthLatitude + ', ' +
                    bb.EastLongitude + ' ' + bb.NorthLatitude + ', ' +
                    bb.WestLongitude + ' ' + bb.NorthLatitude + ', ' +
                    bb.WestLongitude + ' ' + bb.SouthLatitude + '))';
      return polygonWKT;
    }
    
    </script>
    

    Jonathon