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?
Link gekopieerd
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.
Link gekopieerd
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.
Link gekopieerd
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>
?>
Link gekopieerd
Bedankt, het werkt volledig!!!
Link gekopieerd
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);
}
};
?>
Link gekopieerd
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?
Link gekopieerd
Beetje offtopic, maar please .. gebruik niet meer de oeroude en tevens al jarenlang niet meer onderhouden mysql functies, maar kijk naar PDO of MySQLi
Link gekopieerd
Wat moet ik dan gebruiken?
thx voor de hulp
Link gekopieerd