probleem met functie uit te voeren google maps
Ik heb volgens script gevonden op deze site, en ik heb het bijgewerkt voor te kunnen automatiseren.
index.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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');
}
}
?>
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:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&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>
<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?
Gewijzigd op 19/08/2013 07:41:47 door Jenthe Van Buggenhout
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?
Bedankt voor de info, ik heb de code al wat opgekuisd.
De afstand echoen gebeurd volgens mij vie de volgende code:
De bedoeling van het script is dat het automatisch de afstanden berekent tussen de scheidsrechters en de sporthallen via google maps.
<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.
googledistance.class.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?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);
}
};
?>
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:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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>
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>
?>
Gewijzigd op 19/08/2013 13:00:21 door Frank Nietbelangrijk
Bedankt, het werkt volledig!!!
(googledistance.class.php)
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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);
}
};
?>
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);
}
};
?>
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)
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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();
}
}
?>
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
thx voor de hulp
PDO of anders MySQLi
zal wel moeten worden:
Bedankt, en al zeker bedankt voor de hulp ik leer hier enorm veel :)
wat heb je al in de database aan tabellen en kolommen?
Code (php)
1
INSERT INTO klvv_sr_afstand_sh ( vvb_nr_sr, shid, afstand, tijd) VALUES('$vvb', '$shid', '$afstand', '$tijd');
Ik heb de SQL code zelf getest, deze is ok ( rechtstreeks in de mysql workbench )
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$link = @mysqli_connect('localhost', 'user', 'password', 'database');
if (!$link) {
die('Connect Error: ' . mysqli_connect_errno());
}
$query = "INSERT INTO klvv_sr_afstand_sh ( vvb_nr_sr, shid, afstand, tijd) VALUES('".$vvb."', '".$shid."', '".$afstand."', '".$tijd."')";
if(mysqli_query($link, $query))
echo 'success!';
else
echo 'Error: ' . mysqli_error($link);
?>
$link = @mysqli_connect('localhost', 'user', 'password', 'database');
if (!$link) {
die('Connect Error: ' . mysqli_connect_errno());
}
$query = "INSERT INTO klvv_sr_afstand_sh ( vvb_nr_sr, shid, afstand, tijd) VALUES('".$vvb."', '".$shid."', '".$afstand."', '".$tijd."')";
if(mysqli_query($link, $query))
echo 'success!';
else
echo 'Error: ' . mysqli_error($link);
?>
Gewijzigd op 24/08/2013 16:43:08 door Frank Nietbelangrijk