Crunchie
03-09-2010, 05:02 PM
Hi, I imagine this is one of those simpel questions, however having spent many years cutting html, and now trying to update myself with JavaScript and CSS, so please excuse the HTML for being really old school. (Don't see an *oh, I am SO embarassed smily). Following is my first attempt at JavaScript.
Intent : Nice static google map, user selects an area by clicking in the upper left and lower right corners. The system returns the upper left and lower right GPS coordinates as a result. If they click in different corners, such up upper right and lower left, the code automatically swaps everything around.
Problem : The script does well at finding the pixels that were clicked, however the graphics (which are little corner markers) are not put in the right spot. The math to return the GPS coordinates as a result is simple, but I just can't see what I have done wrong in the display.
Regards
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Welcome to Senior Alliance</title>
<script type="text/javascript" >
function GeoFence(event){
pos_x1 = event.offsetX?(event.offsetX):event.pageX-document.getElementById("GeoFenceMap").offsetLeft;
pos_y1 = event.offsetY?(event.offsetY):event.pageY-document.getElementById("GeoFenceMap").offsetTop;
if (pos_x2==0) {
pos_x1_swap = pos_x1;
pos_y1_swap = pos_y1;
document.getElementById("GeoFenceUL").style.left = pos_x1_swap ;
document.getElementById("GeoFenceUL").style.top = pos_y1_swap ;
document.getElementById("GeoFenceUL").style.visibility = "visible" ;
document.pointform.form_x1.value = pos_x1_swap;
document.pointform.form_y1.value = pos_y1_swap;
pos_x2 = pos_x1;
pos_y2 = pos_y1;
}
else
{
pos_x1_swap = pos_x1;
pos_y1_swap = pos_y1;
pos_x2_swap = pos_x2;
pos_y2_swap = pos_y2;
if(pos_x1_swap > pos_x2_swap) {temp=pos_x1_swap;pos_x1_swap=pos_x2_swap;pos_x2_swap=temp;}
if(pos_y1_swap > pos_y2_swap) {temp=pos_y1_swap;pos_y1_swap=pos_y2_swap;pos_y2_swap=temp;}
document.getElementById("GeoFenceUL").style.left = pos_x1_swap ;
document.getElementById("GeoFenceUL").style.top = pos_y1_swap ;
document.getElementById("GeoFenceUL").style.visibility = "visible" ;
document.getElementById("GeoFenceUR").style.left = pos_x1_swap ;
document.getElementById("GeoFenceUR").style.top = pos_y2_swap ;
document.getElementById("GeoFenceUR").style.visibility = "visible" ;
document.getElementById("GeoFenceLL").style.left = pos_x2_swap ;
document.getElementById("GeoFenceLL").style.top = pos_y1_swap ;
document.getElementById("GeoFenceLL").style.visibility = "visible" ;
document.getElementById("GeoFenceLR").style.left = pos_x2_swap ;
document.getElementById("GeoFenceLR").style.top = pos_y2_swap ;
document.getElementById("GeoFenceLR").style.visibility = "visible" ;
document.pointform.form_x1.value = pos_x1_swap;
document.pointform.form_y1.value = pos_y1_swap;
document.pointform.form_x2.value = pos_x2_swap;
document.pointform.form_y2.value = pos_y2_swap;
pos_x2 = pos_x1;
pos_y2 = pos_y1;
}
}
</script>
<script>
var pos_x1_swap;
var pos_y1_swap;
var pos_x2_swap;
var pos_y2_swap;
var pos_x1;
var pos_y1;
var pos_x2=0;
var pos_y2=0;
</script>
</head>
<body>
<table style="background-color: rgb(255, 255, 255); width: 1094px; height: 588px; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="3" cellspacing="3">
<tbody>
<tr>
<td style="width: 30px; vertical-align: bottom;" rowspan="14" background="./Resources/Border_Left.png">___</td>
<td style="width: 360px;"></td>
<td style="width: 142px;"></td>
<td style="width: 432px;"></td>
<td style="width: 70px;"></td>
<td style="width: 30px; vertical-align: bottom;" rowspan="14" background="./Resources/Border_Right.png">__</td>
</tr>
<tr><td>
<input id="CustSalutation" maxlength="5" name="CustSalutation" size="3" type="hidden" value="".$CustSalutation.""/>
</td></tr>
#Zoom level 12 0.000361 -0.000212
#//// Lat per pixel Lon per pixel
<tr><td colspan="4" width="600px" height="600px">
<form name="pointform" method="post">
<div id="GeoFenceMap" onclick="GeoFence(event)" style = "background-image:url('http://maps.google.com/maps/api/staticmap?center=51,-2&zoom=12&size=600x600&maptype=hybrid&sensor=false');width:600px;height:600px;background-repeat:no-repeat;z-index:1;">
<img src="./Resources/UL.gif" id="GeoFenceUL" style="position:absolute;visibility:hidden;z-index:2;">
<img src="./Resources/LL.gif" id="GeoFenceUR" style="position:absolute;visibility:hidden;z-index:2;">
<img src="resources/UR.gif" id="GeoFenceLL" style="position:absolute;visibility:hidden;z-index:2;">
<img src="Resources/LR.gif" id="GeoFenceLR" style="position:absolute;visibility:hidden;z-index:2;"></div>
You pointed at x = <input type="text" name="form_x1" size="4" /> - y = <input type="text" name="form_y1" size="4" /> and had previously pointed at x =<input type="text" name="form_x2" size="4" /> - y = <input type="text" name="form_y2" size="4" />
<br /><br />
</form>
<input class="submit" name="GeoFenceAttempt" value="Save and Continue" type="submit" /><br />
</td></tr>
</body>
</html>
Intent : Nice static google map, user selects an area by clicking in the upper left and lower right corners. The system returns the upper left and lower right GPS coordinates as a result. If they click in different corners, such up upper right and lower left, the code automatically swaps everything around.
Problem : The script does well at finding the pixels that were clicked, however the graphics (which are little corner markers) are not put in the right spot. The math to return the GPS coordinates as a result is simple, but I just can't see what I have done wrong in the display.
Regards
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Welcome to Senior Alliance</title>
<script type="text/javascript" >
function GeoFence(event){
pos_x1 = event.offsetX?(event.offsetX):event.pageX-document.getElementById("GeoFenceMap").offsetLeft;
pos_y1 = event.offsetY?(event.offsetY):event.pageY-document.getElementById("GeoFenceMap").offsetTop;
if (pos_x2==0) {
pos_x1_swap = pos_x1;
pos_y1_swap = pos_y1;
document.getElementById("GeoFenceUL").style.left = pos_x1_swap ;
document.getElementById("GeoFenceUL").style.top = pos_y1_swap ;
document.getElementById("GeoFenceUL").style.visibility = "visible" ;
document.pointform.form_x1.value = pos_x1_swap;
document.pointform.form_y1.value = pos_y1_swap;
pos_x2 = pos_x1;
pos_y2 = pos_y1;
}
else
{
pos_x1_swap = pos_x1;
pos_y1_swap = pos_y1;
pos_x2_swap = pos_x2;
pos_y2_swap = pos_y2;
if(pos_x1_swap > pos_x2_swap) {temp=pos_x1_swap;pos_x1_swap=pos_x2_swap;pos_x2_swap=temp;}
if(pos_y1_swap > pos_y2_swap) {temp=pos_y1_swap;pos_y1_swap=pos_y2_swap;pos_y2_swap=temp;}
document.getElementById("GeoFenceUL").style.left = pos_x1_swap ;
document.getElementById("GeoFenceUL").style.top = pos_y1_swap ;
document.getElementById("GeoFenceUL").style.visibility = "visible" ;
document.getElementById("GeoFenceUR").style.left = pos_x1_swap ;
document.getElementById("GeoFenceUR").style.top = pos_y2_swap ;
document.getElementById("GeoFenceUR").style.visibility = "visible" ;
document.getElementById("GeoFenceLL").style.left = pos_x2_swap ;
document.getElementById("GeoFenceLL").style.top = pos_y1_swap ;
document.getElementById("GeoFenceLL").style.visibility = "visible" ;
document.getElementById("GeoFenceLR").style.left = pos_x2_swap ;
document.getElementById("GeoFenceLR").style.top = pos_y2_swap ;
document.getElementById("GeoFenceLR").style.visibility = "visible" ;
document.pointform.form_x1.value = pos_x1_swap;
document.pointform.form_y1.value = pos_y1_swap;
document.pointform.form_x2.value = pos_x2_swap;
document.pointform.form_y2.value = pos_y2_swap;
pos_x2 = pos_x1;
pos_y2 = pos_y1;
}
}
</script>
<script>
var pos_x1_swap;
var pos_y1_swap;
var pos_x2_swap;
var pos_y2_swap;
var pos_x1;
var pos_y1;
var pos_x2=0;
var pos_y2=0;
</script>
</head>
<body>
<table style="background-color: rgb(255, 255, 255); width: 1094px; height: 588px; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="3" cellspacing="3">
<tbody>
<tr>
<td style="width: 30px; vertical-align: bottom;" rowspan="14" background="./Resources/Border_Left.png">___</td>
<td style="width: 360px;"></td>
<td style="width: 142px;"></td>
<td style="width: 432px;"></td>
<td style="width: 70px;"></td>
<td style="width: 30px; vertical-align: bottom;" rowspan="14" background="./Resources/Border_Right.png">__</td>
</tr>
<tr><td>
<input id="CustSalutation" maxlength="5" name="CustSalutation" size="3" type="hidden" value="".$CustSalutation.""/>
</td></tr>
#Zoom level 12 0.000361 -0.000212
#//// Lat per pixel Lon per pixel
<tr><td colspan="4" width="600px" height="600px">
<form name="pointform" method="post">
<div id="GeoFenceMap" onclick="GeoFence(event)" style = "background-image:url('http://maps.google.com/maps/api/staticmap?center=51,-2&zoom=12&size=600x600&maptype=hybrid&sensor=false');width:600px;height:600px;background-repeat:no-repeat;z-index:1;">
<img src="./Resources/UL.gif" id="GeoFenceUL" style="position:absolute;visibility:hidden;z-index:2;">
<img src="./Resources/LL.gif" id="GeoFenceUR" style="position:absolute;visibility:hidden;z-index:2;">
<img src="resources/UR.gif" id="GeoFenceLL" style="position:absolute;visibility:hidden;z-index:2;">
<img src="Resources/LR.gif" id="GeoFenceLR" style="position:absolute;visibility:hidden;z-index:2;"></div>
You pointed at x = <input type="text" name="form_x1" size="4" /> - y = <input type="text" name="form_y1" size="4" /> and had previously pointed at x =<input type="text" name="form_x2" size="4" /> - y = <input type="text" name="form_y2" size="4" />
<br /><br />
</form>
<input class="submit" name="GeoFenceAttempt" value="Save and Continue" type="submit" /><br />
</td></tr>
</body>
</html>