Forum Moderators: coopster

Message Too Old, No Replies

Can this be done in PHP?

can Javascript function be rewritten in php?

         

thewaiter

3:29 am on Dec 18, 2008 (gmt 0)

10+ Year Member



Hello,
I started this thread in Webmaster General:
[webmasterworld.com...]
Quickly, my goal was to find a way for me to determine if a latitude/longitude marker falls within a polygon. I found a couple of Point in Polygon 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 a number of markers fall within a polygon or if I can tweak the javascript so instead of clicking on the map, it can just take an array of markers and let me know which ones fall within the polygon and which ones don't.

I apologize if it's confusing (it is to me). If anyone can help I would appreciate it. Here is my 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[i].Longitude<lon && points[j].Longitude>=lon ¦¦ points[j].Longitude<lon && points[i].Longitude>=lon)
{
if (points[i].Latitude+(lon-points[i].Longitude)/(points[j].Longitude-points[i].Longitude)*(points[j].Latitude-points[i].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>

coopster

1:44 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I apologize if it's confusing (it is to me).

Perhaps a little. It looks like a pretty advanced project. You can indeed adapt code from one language to another so if it works in JavaScript you should certainly be able to rewrite the application in PHP.