kelliinpb Posted December 19, 2008 Share Posted December 19, 2008 Hello, I am looking for a solution to a point in polygon problem. My goal is to find a way for me to determine if a latitude/longitude marker falls within a polygon. I found a couple of PiP solutions, neither were completely what I was looking for but they were close. One is based on JavaScript and it takes a Virtual Earth map, displays the polygon using my co-ordinates and if I click inside the polygon, an alert box returns true, if I click outside the polygon, it returns false. I don't know enough about JavaScript to determine if this can be adapted into PHP where it just runs a loop and returns if an array of markers fall within a polygon. I apologize if it's confusing (it is to me). If anyone can help I would appreciate it. Here is the javascript code and thanks for everything: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="http://example.com/mapcontrol/mapcontrol.ashx?v=6.1"></script> <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> </head> <body onload="GetMap();"> <div id='myMap' style="position: relative; width: 600px; height: 400px;"></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/137635-can-this-be-done-in-php/ Share on other sites More sharing options...
.josh Posted December 19, 2008 Share Posted December 19, 2008 Wow, small world. Someone else was looking for almost exactly the same thing recently. Quote Link to comment https://forums.phpfreaks.com/topic/137635-can-this-be-done-in-php/#findComment-719406 Share on other sites More sharing options...
kelliinpb Posted December 19, 2008 Author Share Posted December 19, 2008 Hi, I checked around and saw this thread: http://www.phpfreaks.com/forums/index.php/topic,108445.0.html The final suggestion was to draw lines from my point to see how many times it intersects with the polygon, if it intersects an odd number of times, it's inside, an even number, it's outside. However I have no idea how to do this. I have a working example in Javascript and I don't know enough about Javascript and not having a good enough understanding in math to figure out how to do this and am looking for a new direction. I thought with my code and trying to go from javascript to php I would post it under a new topic. Quote Link to comment https://forums.phpfreaks.com/topic/137635-can-this-be-done-in-php/#findComment-719437 Share on other sites More sharing options...
Mchl Posted December 19, 2008 Share Posted December 19, 2008 Honestly, I would probably have some hard time coding it by myself. It's just an old algorithm I know from math classes. You want it to work on map or on a plane? Quote Link to comment https://forums.phpfreaks.com/topic/137635-can-this-be-done-in-php/#findComment-719492 Share on other sites More sharing options...
kelliinpb Posted December 21, 2008 Author Share Posted December 21, 2008 What I would like this to do is to just loop through the points of the polygon and check it against a longitude/latitude point and tell me if my point falls inside or outside the polygon. Like I said, a JavaScript master I am not, but what really confuses me is in the IsInPolygon function. I have no idea what j=points.length-1 means or what points.Longitude<lon && points[j].Longitude>=lon means. Because after that, it should just be a simple task of looping through the array, separating the latitude and longitude values and running through this math: 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; } Right? They set the inPoly variable to false so if it passes through both those if statements, then the point I gave it must fall within the polygon? Quote Link to comment https://forums.phpfreaks.com/topic/137635-can-this-be-done-in-php/#findComment-720605 Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 1 min you copy and past a code then you want us to sort it out lol, well sorry but what your asking is a job for the freelance forum on here, it will take more then what your asking sorry. Quote Link to comment https://forums.phpfreaks.com/topic/137635-can-this-be-done-in-php/#findComment-720622 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.