Hello,
I'm developing a web application that makes use of the Google Maps API to map farms, and highlight them with different colors according to the level of a particular pest.
I need to capture the user click within the selected area as shown in the image below, I found some algorithms able to calculate the click location but I am in doubt to apply it. I'm using JavaScript.
How much click inside the area, I want to do a certain action, my question is: how to capture this click, using the Google Maps API, within a specific area mapped by latitude and longitude? >
AlgorithmthatIfoundtocalculatethepointwithinthearea:
window.isPointInPolygon = function(pontoClique, arrayCoordenadas) {
var intersectCount = 0;
for (var j = 0; j < arrayCoordenadas.size() - 1; j++) {
if (rayCastIntersect(pontoClique, arrayCoordenadas.get(j), arrayCoordenadas.get(j + 1))) {
intersectCount++;
}
}
return ((intersectCount % 2) == 1); // odd = inside, even = outside;
}
var rayCastIntersect = function(pontoClique, LatLngVertA, LatLngVertB) {
var aY = LatLngVertA.lat;
var bY = LatLngVertB.lat;
var aX = LatLngVertA.lng;
var bX = LatLngVertB.lng;
var pY = pontoClique.lat;
var pX = pontoClique.lng;
if ((aY > pY && bY > pY) || (aY < pY && bY < pY) || (aX < pX && bX < pX)) {
return false; // a and b can't both be above or below pt.y, and a or
// b must be east of pt.x
}
var m = (aY - bY) / (aX - bX); // Rise over run
var bee = (-aX) * m + aY; // y = mx + b
var x = (pY - bee) / m; // algebra is neat!
return x > pX;
}