Reputation: 1828
Few days ago, I made a C# function, to detect if a point in inside of a polygon. After some drawbacks, I finally made the function. But after using for first time, I found that approach was very slower. Call a C# code behind function, from javascript, is very troublesome, since javascript continues the execution of the code, and don't wait for the result of the C# code.
Below, I posted the solution for this.
Upvotes: 2
Views: 708
Reputation: 1828
I was forced to construct a new function, this time in plain javascript. After browsing a while, I found a couple of examples (using Google maps functions) and another in a Microsoft site (http://msdn.microsoft.com/en-us/library/cc451895.aspx), but the function contained in the MS site always return false. Then, using as reference a code posted here in Stackoverflow, now I have a efficient javascript function, adapted to Bing maps AJAX v7. I hope can be useful for others. Don't forget to mark as useful!
loc = pushPin.getLocation();
var isInside = false;
var j = 0;
var x = loc.longitude;
var y = loc.latitude;
var paths = polygon.getLocations();
for (var i = 0; i < paths.length ; i++) {
j++;
if (j == paths.length) { j = 0; }
if (((paths[i].latitude < y) && (paths[j].latitude >= y))
|| ((paths[j].latitude < y) && (paths[i].latitude >= y))) {
if (paths[i].longitude + (y - paths[i].latitude)
/ (paths[j].latitude - paths[i].latitude)
* (paths[j].longitude - paths[i].longitude) < x) {
isInside = !isInside
}
}
}
if (isInside == true) { pushPin.setOptions({ visible: true }); };
Upvotes: 6