Hallo,

Ik heb volgens script gevonden op deze site, en ik heb het bijgewerkt voor te kunnen automatiseren.

index.php

<?php
include('../sec/inc_mysql_connect.php');
$sql = "SELECT VVBnummer, Adres, Postcode FROM tblscheidsrechters";// echo($sql);
$result = mysql_query($sql);

$sql_sh = "SELECT ID, SporthalAdres, Postcode FROM tblsporthal"; //echo('<br>' . $sql_sh);
$result_sh = mysql_query($sql_sh);
while($record = mysql_fetch_array($result))
{
while($record_sh = mysql_fetch_array($result_sh))
{
$fromAddress = $record['Adres'] . ',' . $record['Postcode']; echo($fromAddress . '<br>');
$toaddress = $record_sh['SporthalAdres'] . ',' . $record_sh['Postcode']; echo($toaddress . '<br>');
include('afstand.html');
}
}
?>

en dan uiteraard afstand.html:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=AIzaSyA_F6jvnChhojxeCahfRF1okNB9LOl11oU" type="text/javascript"></script> 
<script type="text/javascript">
function initialize() 
{ 
if (GBrowserIsCompatible()) 
{ 
// nieuw object ZONDER parameters, want we willen nl. geen geschreven route EN geen map tonen.... 
// http://code.google.com/apis/maps/doc...ml#GDirections 
gdir = new GDirections(); 
GEvent.addListener(gdir,"load", set_distance); 
} 
} 

function setDirections(fromAddress, toAddress, locale) 
{ 
// http://code.google.com/apis/maps/doc...ectionsOptions 
gdir.load("from: " + fromAddress + " to: " + toAddress, {locale: locale, travelMode:G_TRAVEL_MODE_DRIVING,avoidHighways: false} ); 
} 

function set_distance() 
{ 
// extra: foutafhandeling, kan handig zijn!!! 
handleErrors(); 
// document.getElementById("afstand").innerHTML = (gdir.getDistance().meters); 

var adl_afstand = (gdir.getDistance().meters)/500 
adl_afstand = Math.round(adl_afstand * 1,35) 



aant_km = 'Het aantal km is: ' + adl_afstand + ' km'

document.getElementById("ritprijs").innerHTML = aant_km; 
} 

</script>
</head> 
<body onload="initialize()" onunload="GUnload()"> 
<script language="javascript">setDirections(<?php echo($fromAddress) ?>, <?php echo($toaddress) ?>, 'BE'); </script>
</body>

Ik krijg enkel de adressen te zien ( echo's), geen afstanden van het javascript.
Kan er iemand mij helpen?
graag alleen relevante code plaatsen en deze tussen code tags.. zo is het niet leesbaar en zul je niet snel hulp krijgen. Ik denk dat je die afstanden wel moet echoen naar het scherm wil je ze zien nu doe je het alleen met adressen. Wat je doel ook is met het scriptje is me ook niet duidelijk. kan je dit even uitleggen?
Hallo,

Bedankt voor de info, ik heb de code al wat opgekuisd.
De afstand echoen gebeurd volgens mij vie de volgende code:

document.getElementById("ritprijs").innerHTML = aant_km;

De bedoeling van het script is dat het automatisch de afstanden berekent tussen de scheidsrechters en de sporthallen via google maps.

en heb je nu ergens staan in je pagina

<div id="ritprijs"></div>


en hoezo eigenlijk ritprijs? Je kan denk ik beter even de API van google erbij pakken dat werkt een stuk makkelijker dan een ander script te kopieren want nu heb je volgens mij veel onnodige dingen erin staan.

Een mooiere oplossing is met PHP en CURL.

googledistance.class.php:

<?php

class GoogleDistance
{
	private $obj;
	
	function __construct($origin, $destination)
	{
		$this->obj = $this->run($origin, $destination);
	}
	
	public function getObject()
	{
		return $this->obj;
	}
	
	public function getOrigin()
	{
		return $this->obj->origin_addresses[0];
	}
	
	public function getDestination()
	{
		return $this->obj->destination_addresses[0];
	}

	public function getDistance()
	{
		return $this->obj->rows[0]->elements[0]->distance->value;
	}
	
	public function getDuration()
	{
		return $this->obj->rows[0]->elements[0]->duration->value;
	}
	
	private function run($origin, $destination)
	{	 
		$path = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' . 
			$this->formatstring($origin) . '&destinations=' . 
			$this->formatstring($destination) .'&language=nl=NLR&sensor=false';
	 	 
		// our curl handle (initialize ifrequired)
		static $ch = null;
		if (is_null($ch)) {
			$ch = curl_init();
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_URL, $path);
		curl_setopt($ch, CURLOPT_HEADER, 0);
	 
		// run the query
		$res = curl_exec($ch);
		if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
		$dec = json_decode($res);
		if(!$dec)
		{
			echo '---><pre>';
			print_r($res);
			echo '</pre>';
			throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
		}
		return $dec;
	}
	
	private function formatstring($text)
	{
		return str_replace(' ', '+', $text);
	}

};
?>


index.php:

<?php

include 'googledistance.class.php';

$fromAddress = "Noordeinde 68, 2514 GL Den Haag";
$toaddress = "Dam , 1012 JS Amsterdam";

$gd = new GoogleDistance($fromAddress, $toaddress);

?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Google distance</title>
    </head> 
    <body> 
    	<p>Afstand in meters: <?php echo $gd->getDistance(); ?></p>
    	<p>Tijd in seconden: <?php echo $gd->getDuration(); ?></p>
    	<p>Gevonden oorsprong: <?php echo $gd->getOrigin(); ?></p>
    	<p>Gevonden bestemming: <?php echo $gd->getDestination(); ?></p>
        <p>Het hele object: <pre><?php print_r($gd->getObject()); ?></pre></p>
    </body>
</html>


?>
Er stond nog een kleine typo in de class waardoor de resultaten in het engels terugkwamen ipv het nederlands. Hier komt ie nog een keer;

(googledistance.class.php)
<?php

class GoogleDistance
{
private $obj;

function __construct($origin, $destination)
{
$this->obj = $this->run($origin, $destination);
}

public function getObject()
{
return $this->obj;
}

public function getOrigin()
{
return $this->obj->origin_addresses[0];
}

public function getDestination()
{
return $this->obj->destination_addresses[0];
}

public function getDistance()
{
return $this->obj->rows[0]->elements[0]->distance->value;
}

public function getDuration()
{
return $this->obj->rows[0]->elements[0]->duration->value;
}

private function run($origin, $destination)
{
$path = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' .
$this->formatstring($origin) . '&destinations=' .
$this->formatstring($destination) .'&language=nl_NL&sensor=false';

// our curl handle (initialize ifrequired)
static $ch = null;
if (is_null($ch)) {
$ch = curl_init();
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_HEADER, 0);

// run the query
$res = curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
$dec = json_decode($res);
if(!$dec)
{
echo '<pre>';
print_r($res);
echo '</pre>';
throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
}
return $dec;
}

private function formatstring($text)
{
return str_replace(' ', '+', $text);
}

};

?>
Hallo,

ik heb nu een andere foutmelding:
Warning: Wrong parameter count for mysql_query() in /home/klvv/public_html/test/index.php on line 3

De bedoeling is nu dat het script de afstanden naar een data bank schrijft, daar loopt het opnieuw fout. ( ik heb nog niet met objecten of OO programmeren gewerkt)


<?php
 include('../sec/inc_mysql_connect.php');
 include 'googledistance.class.php';
 $sql = "SELECT VVBnummer, Adres, Postcode FROM tblscheidsrechters";// echo($sql);
 $result = mysql_query($sql);
 
 $sql_sh = "SELECT ID, SporthalAdres, Postcode FROM tblsporthal"; //echo('<br>' . $sql_sh);
 $result_sh = mysql_query($sql_sh);
 while($record = mysql_fetch_array($result))
 {
  while($record_sh = mysql_fetch_array($result_sh))
  {
   $fromAddress = $record['Adres'] . ',' . $record['Postcode']; //echo($fromAddress . '<br>');
   $toaddress = $record_sh['SporthalAdres'] . ',' . $record_sh['Postcode']; //echo($toaddress . '<br>');
   
   $gd = new GoogleDistance($fromAddress, $toaddress);
   
   $vvb = $record['VVBnummer'];
   $shid =  $record_sh['ID'];
   $afstand = $gd->getDistance();
   $tijd = $gd->getDuration();
   
   
   ?>
   <body> 
        <p>Scheidsrechter: <?php echo($record['VVBnummer']); ?></p>
        <p>Sporthal: <?php echo($record_sh['ID']); ?></p>        
        <p>Afstand in km: <?php echo ($gd->getDistance())/1000 ; ?></p>
        <p>Tijd in minuten: <?php echo ($gd->getDuration())/60; ?></p>
        <p>Gevonden oorsprong: <?php echo $gd->getOrigin(); ?></p>
        <p>Gevonden bestemming: <?php echo $gd->getDestination(); ?></p>
        <hr />
    </body>
   <?php
   $sql = "INSERT INTO klvv_sr_afstand_sh ( vvb_nr_sr, shid, afstand, tijd) VALUES('$vvb', '$shid', '$afstand', '$tijd')"; echo($sql);
   $record = mysql_query();
  }
 }
?>


Weet er iemand waar het nu fout loopt, en wat de oplossing is?
Beetje offtopic, maar please .. gebruik niet meer de oeroude en tevens al jarenlang niet meer onderhouden mysql functies, maar kijk naar PDO of MySQLi
PDO of anders MySQLi

Reageren