Hi everyone, I’m not a JavaScript person, PHP I can get by with, JavaScript no. I’m trying to help out a friend with a point in polygon problem and the solution I have is in JavaScript but I’m trying to rewrite it in PHP and just need a few things to be explained if you don’t mind.
The script works as such, I give it the boundaries for a polygon, I click on the map and it tells me if I clicked inside or outside the polygon. I am trying to get it so I can give it several points and it just loops through the function and tells me if the various points are inside or outside the polygon.
Here is the script and I’ll tell you where I’m having problems:
<script type=”text/javascript”>
var map = null;
var polygon = null;
function GetMap() {
map = new VEMap(‘myMap’);
map.LoadMap();
//Plot Polygon
var points = new Array(
new VELatLong(41.7972300222736, -87.7281369157155),
new VELatLong(41.7983290222745, -87.7281489157156),
new VELatLong(41.798857022275, -87.7267849157143),
new VELatLong(41.7989060222752, -87.7266109157142),
new VELatLong(41.7990980222752, -87.7260359157136),
new VELatLong(41.7991840222754, -87.7258169157134),
new VELatLong(41.8001330222763, -87.7233909157112)
);
polygon = new VEShape(VEShapeType.Polygon, points);
polygon.HideIcon();
map.AddShape(polygon);
map.SetMapView(points);
//Add onclick handler
map.AttachEvent(“onclick”, map_click);
}
function map_click(eventArgs) {
var latlong = map.PixelToLatLong(new VEPixel(eventArgs.mapX, eventArgs.mapY));
alert(“Is Point Within Polyline:n” + GeoHelper.IsInPolygon(polygon.GetPoints(), latlong));
}
if (GeoHelper == undefined)
var GeoHelper = {};
GeoHelper.IsInPolygon=function(points,latlong) {
var i;
var j=points.length-1;
var inPoly=false;
var lat = latlong.Latitude;
var lon = latlong.Longitude;
for (i=0; i<points.length; i++) {
if (points.Longitude<lon && points[j].Longitude>=lon ¦¦ points[j].Longitude<lon && points.Longitude>=lon) {
if (points.Latitude+(lon-points.Longitude)/(points[j].Longitude-points.Longitude)*(points[j].Latitude-points.Latitude)<lat) {
inPoly=!inPoly;
}
}
j=i;
}
return inPoly;
};
</script>
In the IsInPolygon function, I know the var i is to start the loop but I don’t know what the var j is for. The [B]points.length-1
[B]points.Longitude<lon && points[j].Longitude>=lon ¦¦ points[j].Longitude<lon && points.Longitude>=lon
And it’s the points[j].Longitude that again confuses me because of the [j], I’m not sure what that’s telling me.
And I don’t know what the j=i at the end of the for loop is for. They already returned the inPoly=!inPoly.
Sorry if I sound like an idiot, I realize it sounds a little stupid, I just have no idea what that means and I would like to get this finished and out of my head.
Thanks for any help you can give.