Hey all,
I've come across quite a hurdle... and want to get some suggestions on how it should be tackled. Let me lay it out for you all...
I have a PHP function which loops through a MySQL result set. I have also developed a javascript function which uses the Google Maps API to get the distance in metres of two given addresses:
<script type="text/javascript">
function getLength(from, to) {
geocoder = new GClientGeocoder();
geocoder.getLatLng(from,function(from_gl) {
if (!from_gl) {
//alert(from + " not found");
Set_Cookie('bookmein_distance', result, 1, '/', '', '');
}
else {
geocoder.getLatLng(to,function(to_gl) {
if (!to_gl) {
//alert(to + " not found");
Set_Cookie('bookmein_distance', result, 1, '/', '', '');
}
else {
//alert ("Both addresses were found!");
result = (to_gl.distanceFrom(from_gl));
//alert(result);
//document.getElementById("main").innerHTML = result;
//return (result);
Set_Cookie('bookmein_distance', result, 1, '/', '', '');
//document.write(result);
}
});
}
});
}
window.onload=function(){
getLength("<?php echo $_GET["from"]; ?>","<?php echo $_GET["to"]; ?>");
//myResult = getLength("<?php echo $_GET["from"]; ?>","<?php echo $_GET["to"]; ?>");
//alert (myResult);
//Set_Cookie('distance_result', myResult, 1, '/', '', '');
}
function Set_Cookie( name, value, expires, path, domain, secure ) {
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + "=" +escape (value) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
Now, the problem is that on each iteration of results from the MySQL query, I want to call this javascript function using addresses returned from the query, have the javascript value passed to a php variable and use it as I would do in that function. And to answer the obvious question, it doesn't seem there is a PHP equivalent API to google maps...
I've moved the javascript function to a separate page, as you'll see in the code, the function is called onload, with PHP to take the from/to from the URL. Opening the page on it's own - for some reason document.write()/writeln() and println() fails to put the result to screen, however I've tested that it sets the cookie just fine.
Foolishly, I thought that I could open the URL from the PHP function using fopen(), and then using $_COOKIE[...] to get the returned result, but of course this function just opens a file stream, so yeah, not that simple.
So, where to next. I've found http://satoewarna.com/jqsajax/, but don't want to go to such lengths just to make this functional... Would like to ask - has anyone used the function http_get(), and by doing so does it invoke the URL's javascript?
Thanks all,
Jess.