none
Bingmaps Version 7 RRS feed

  • Question

  • Is there any Webservices for PointinPolygon method in bingmaps version 7..

    Appreciate the help!!

    Thanks

     

    • Moved by Ricky_Brundritt Friday, March 9, 2012 5:10 PM (From:Bing Maps: Map Control and Web services Development)
    Wednesday, April 20, 2011 5:34 PM

Answers

  • I'm not aware of any webservices that offer a method to determine whether a point lies within a polygon but, to be honest, a webservice wouldn't be a very efficient way of providing such functionality anyway - assuming you've already got the points that define the polygon, and the point to tested on the client-side app already, you can do a p-i-p query in javascript directly rather than sending all that information to a remote webservice.

    The following example extends the polygon class with a containsLocation() method and demonstrates the usage of that method to test whether a draggable pushpin is contained within various polygons on the map:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <title></title>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
     <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
    
     <script type="text/javascript">
      var map = null;
      var PolygonLayer = new Microsoft.Maps.EntityCollection();
      var PointLayer = new Microsoft.Maps.EntityCollection();
      
      // Extend the Polygon class to include a containsLocation method
      Microsoft.Maps.Polygon.prototype.containsLocation = function(Location) {
       var Locations = this.getLocations();
       var inPoly = false;
       var i;
       var j = Locations.length - 1;
       // Use raycasting to determine if the location lies within the polygon
       for (var i = 0; i < Locations.length; i++) {
        var vertex1 = Locations[i];
        var vertex2 = Locations[j];
        if (vertex1.longitude < Location.longitude && vertex2.longitude >= Location.longitude || vertex2.longitude < Location.longitude && vertex1.longitude >= Location.longitude) {
         if (vertex1.latitude + (Location.longitude - vertex1.longitude) / (vertex2.longitude - vertex1.longitude) * (vertex2.latitude - vertex1.latitude) < Location.latitude) {
          inPoly = !inPoly;
         }
        }
        j = i;
       }
       return inPoly;
      };
    
      function GetMap() {
       // Create a basic map
       var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),
        { credentials: "YOUR BING MAPS KEY",
         center: new Microsoft.Maps.Location(30, 20),
         mapTypeId: Microsoft.Maps.MapTypeId.aerial,
         labelOverlay: Microsoft.Maps.LabelOverlay.hidden,
         zoom: 3
        });
    
       // Create a polygon with a hole
       var Polygon = new Microsoft.Maps.Polygon(
    		 	[ new Microsoft.Maps.Location(-10, -20), new Microsoft.Maps.Location(10, -20), new Microsoft.Maps.Location(10, 20), new Microsoft.Maps.Location(-10, 20), new Microsoft.Maps.Location(-10, -20), //outer ring
    		 		new Microsoft.Maps.Location(-5, -10), new Microsoft.Maps.Location(-5, 10), new Microsoft.Maps.Location(5, 0), new Microsoft.Maps.Location(-5, -10), new Microsoft.Maps.Location(-10, -20)], //inner ring
    		 	{ fillColor: new Microsoft.Maps.Color(128, 128, 0, 0)	}
    		 );
    		 PolygonLayer.push(Polygon);
    		 	
    		 // Create another polygon
    		 var Polygon2 = new Microsoft.Maps.Polygon(
    		 	[	new Microsoft.Maps.Location(50, -6),new Microsoft.Maps.Location(51.5, 2),	new Microsoft.Maps.Location(59, -3.25), new Microsoft.Maps.Location(56, -6), new Microsoft.Maps.Location(50, -6)],
    		 	{ fillColor: new Microsoft.Maps.Color(128, 128, 0, 0)	}
    		 );
    		 	PolygonLayer.push(Polygon2);
    
    		 	// Create another polygon
    		 	var Polygon3 = new Microsoft.Maps.Polygon(
    		 	[new Microsoft.Maps.Location(30, 32), new Microsoft.Maps.Location(13, 44), new Microsoft.Maps.Location(22, 60), new Microsoft.Maps.Location(26, 56), new Microsoft.Maps.Location(24, 51), new Microsoft.Maps.Location(35, 44), new Microsoft.Maps.Location(30, 32)],
    		 	{ fillColor: new Microsoft.Maps.Color(128, 128, 0, 0) }
    		 );
    		 	PolygonLayer.push(Polygon3);
    		 
    		 // Add the polygon layer to the map
       map.entities.push(PolygonLayer);
    
       // Create a draggable Pushpin
       var pin = new Microsoft.Maps.Pushpin(
    		  map.getCenter(),
    		  {
    		   draggable: true,
    		   text: "Drag!",
    		  }
    	  );
    	  // Add a handler to the pin drag event
       Microsoft.Maps.Events.addHandler(pin, 'drag', DragPin);
    		 // Add the pushpin to the map
    		 PointLayer.push(pin);
    		 map.entities.push(PointLayer);
    		}
      
      // Handler when the pin is dragged
    		function DragPin(e) {
       // Loop through the polygons
       for(var x = 0; x < PolygonLayer.getLength(); x++) {
        // Check whether this polygon contains the pushpin
        var Poly = PolygonLayer.get(x);
        if(Poly.containsLocation(e.entity.getLocation())) {
         // Green
         Poly.setOptions( {fillColor: new Microsoft.Maps.Color(128, 0, 128, 0)} );
        }
        else {
         // Red
         Poly.setOptions( {fillColor: new Microsoft.Maps.Color(128, 128, 0, 0)} );
        }	
       }
      }
     </script>
    </head>
    <body onload="GetMap();">
     <div id='mapDiv' style="position: relative; width: 1024px; height: 768px;">
     </div>
    </body>
    </html>
    


    twitter: @alastaira blog: http://alastaira.wordpress.com/
    Wednesday, April 20, 2011 5:47 PM
    Moderator