Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
I am trying to get the distance and duration into a php variable so I can call it and display it later.
its currently in a javascript.
code:
function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
Any ideas.
i want like $distance = the output that is produced about
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
you want to rewrite that distance formula in php?
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
I am using the google maps API and that statement produces the required output I need.
But I need to be able to call the function in php afterwards.
so i can carry the following for example.
$time = (whatever you do);
$distance = (whatever you do);
$time+$distance*100 = $price
echo $price;
So need to get the distance from the script after it is run into php.
[Edited on 10-01-2008 by Jodi_the_g]
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
firstly you will need the form to action a php page, then the php page to get the variables from the form
something like
$distance = htmlentities($_REQUEST['distance']);
$duration = htmlentities($_REQUEST['duration']);
heres a forumla i use on vagweb
code: $distance=sqrt(pow(($userx-$homex),2)+pow(($usery-$homey),2))/1000;
$distanceadjust = $distance * 0.19;
$distance= $distance + $distanceadjust;
$distancekm=round($distance, 0);
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
I have the form already
code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>A Class Corporate Travel</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAN7396AA-yYP5YWcjnhyRHBSPvi7yoDxBrMWr3cNIOhTzGhtskhSIzUH_dF60NfPSUgM_KJPNo6uvHg"
type="text/javascript"></script>
<style type="text/css">
body {
font-family: Verdana, Arial, sans serif;
font-size: 11px;
margin: 2px;
}
table.directions th {
background-color:#EEEEEE;
}
img {
color: #000000;
}
</style>
<script type="text/javascript">
var map;
var gdir;
var geocoder = null;
var addressMarker;
var directions;
var dis;
var distance;
var duration;
var getstartgeocode;
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
gdir = new GDirections(map, document.getElementById("directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);
}
}
function setDirections(fromAddress, toAddress, en) {
gdir.load("from: " + fromAddress + ", UK" + " to: " + toAddress + ", UK",
{ "locale": en });
}
function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is incorrect, please try again.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
else alert("An unknown error occurred.");
}
function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
}
<?php
$distance = distance
$duration = duration
?>
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<form action="#" onsubmit="setDirections(this.from.value, this.to.value); return false">
<div align="center">
<table>
<tr><th align="right">From: </th>
<td><p>
<input type="text" size="25" id="fromAddress" name="from"
value="Marlow"/>
</p> </td>
<th align="right"> To: </th>
<td align="right"><input type="text" size="25" id="toAddress" name="to"
value="Bicester" /></td></tr>
<tr><th>
<label> </label></th>
<td colspan="3"><div align="right">
<input name="submit" type="submit" value="Submit" />
</div></td></tr>
</table>
</div>
</form>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td>
<div id="distance" align="center"></div><br />
<div id="duration" align="center"></div>
<div id="map" style="width: 0px; height: 0px; visibility:hidden" align="center"></div>
<div id="directions" style="width: 0px; heightpx; visibility:hidden" ></div>
<p align="center">
<img src="http://www.google.com/intl/en_ALL/mapfiles/poweredby.png" unselectable="on" galleryimg="no" />
</p>
</td></tr></table>
</body>
</html>
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
and it works and pulls out the information I need I just need the distance and duration valves to be stored in php after it is run, if you get me?
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
yeah when you submit that form it calls the javascript function, if you want to send the variables to php the action="#" will need to be action="somepage.php"
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
you might be better off putting the javascript in a php page and echo'ing it out when when you call the php page, without testing it though i dont know if that will work properly
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
quote: Originally posted by Steve
you might be better off putting the javascript in a php page and echo'ing it out when when you call the php page, without testing it though i dont know if that will work properly
thats what I am trying to do just can't figure out how to echo the output.
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
I have now got index.php
code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>A Class Corporate Travel</title>
<style type="text/css">
body {
font-family: Verdana, Arial, sans serif;
font-size: 11px;
margin: 2px;
}
table.directions th {
background-color:#EEEEEE;
}
img {
color: #000000;
}
</style>
</head>
<body onload="initialize()" onunload="GUnload()">
<form action="MapAPI.class.php" onsubmit="setDirections(this.from.value, this.to.value); return false">
<div align="center">
<table>
<tr><th align="right">From: </th>
<td><p>
<input type="text" size="25" id="fromAddress" name="from"
value="Marlow"/>
</p> </td>
<th align="right"> To: </th>
<td align="right"><input type="text" size="25" id="toAddress" name="to"
value="Bicester" /></td></tr>
<tr><th>
<label> </label></th>
<td colspan="3"><div align="right">
<input name="submit" type="submit" value="Submit" />
</div></td></tr>
</table>
</div>
</form>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td>
<div id="distance" align="center"></div><br />
<div id="duration" align="center"></div>
<div id="map" style="width: 0px; height: 0px; visibility:hidden" align="center"></div>
<div id="directions" style="width: 0px; heightpx; visibility:hidden" ></div>
<p align="center">
<img src="http://www.google.com/intl/en_ALL/mapfiles/poweredby.png" unselectable="on" galleryimg="no" />
</p>
</td></tr></table>
</body>
</html>
and MapAPI.class.php
code:
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAN7396AA-yYP5YWcjnhyRHBSPvi7yoDxBrMWr3cNIOhTzGhtskhSIzUH_dF60NfPSUgM_KJPNo6uvHg"
type="text/javascript"></script>
<script type="text/javascript">
var map;
var gdir;
var geocoder = null;
var addressMarker;
var directions;
var dis;
var distance;
var duration;
var getstartgeocode;
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
gdir = new GDirections(map, document.getElementById("directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);
}
}
function setDirections(fromAddress, toAddress, en) {
gdir.load("from: " + fromAddress + ", UK" + " to: " + toAddress + ", UK",
{ "locale": en });
}
function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is incorrect, please try again.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
else alert("An unknown error occurred.");
}
function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
}
</script>
and it seems to run ok without any errors but now can not see the output.
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
if you put the html form elements in the php page and echo them out at the same time it should work.
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
il have to look at this later as iv got to go and do a job now
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
quote: Originally posted by Steve
if you put the html form elements in the php page and echo them out at the same time it should work.
I confussed now.
you mean run the script seperate on sumbit then echo back?
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
i mean output the whole page from a php echo
make the form element action the same php page and trap the variables when its submitted
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
So sumbit the form to a different page with the script on then echo that whole script?
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
Am trying this but it errors.
code:
<?php
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAN7396AA-yYP5YWcjnhyRHBSPvi7yoDxBrMWr3cNIOhTzGhtskhSIzUH_dF60NfPSUgM_KJPNo6uvHg"
type="text/javascript"></script>
echo <script type="text/javascript">
var map;
var gdir;
var geocoder = null;
var addressMarker;
var directions;
var dis;
var distance;
var duration;
var getstartgeocode;
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
gdir = new GDirections(map, document.getElementById("directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);
}
}
function setDirections(fromAddress, toAddress, en) {
gdir.load("from: " + fromAddress + ", UK" + " to: " + toAddress + ", UK",
{ "locale": en });
}
function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is incorrect, please try again.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
else alert("An unknown error occurred.");
}
function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
}
echo </script>
?>
but does not work
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
Pretty much tried everything now.
And still can not get the distance and duration stored in php.
|
drax
Member
Registered: 5th Feb 05
Location: Sittingbourne, Kent
User status: Offline
|
Php has to be actioned, you cant run it inline like that, the page has to be refreshed, or the information sent to the script,
Steve already showed you this, you said you have a form, which is great but this has to send information to a php page which can take the information and send it back for the javascript to process (if you're still going to us the JS)
|
Jodi_the_g
Member
Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
|
I speratored the form from the script.
then added the following as steve suggested
code:
function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
}
</script>
<?php
$distance = htmlentities($_REQUEST['distance']);
$duration = htmlentities($_REQUEST['duration']);
?>
but it errors in the php bit with Notice: Undefined index: duration
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
dude, email me the stuff and when i get in i'll have a look.
|