probleem met functie uit te voeren google maps

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Ventilatiesysteem Productontwikkelaar HBO WO Verwa

Samengevat: Zij bieden flexibele ventilatiematerialen, geluidsdempers, rookgasafvoer producten en industrieslangen. Ben jij een technisch productontwikkelaar? Heb jij ervaring met het ontwikkelen van nieuwe producten? Vaste baan: Technisch Productontwikkelaar HBO WO €3.000 - €4.000 Zij bieden een variëteit aan flexibele ventilatiematerialen, geluiddempers, rookgasafvoer producten, industrieslangen en ventilatieslangen voor de scheepsbouw. Met slimme en innovatieve materialen zorgen wij voor een gezonde en frisse leefomgeving. Deze werkgever is een organisatie die volop in ontwikkeling is met hardwerkende collega's. Dit geeft goede ontwikkelingsmogelijkheden. De branche van dit bedrijf is Techniek en Engineering. Functie: Voor de vacature als Technisch Productontwikkelaar Ede Gld HBO WO ga

Bekijk vacature »

Jenthe Van Buggenhout

Jenthe Van Buggenhout

18/08/2013 17:14:15
Quote Anchor link
Hallo,

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

index.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
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');
}
}

?>


en dan uiteraard afstand.html:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<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?
Gewijzigd op 19/08/2013 07:41:47 door Jenthe Van Buggenhout
 
PHP hulp

PHP hulp

21/09/2024 05:24:19
 
Reshad F

Reshad F

18/08/2013 23:38:43
Quote Anchor link
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?
 
Jenthe Van Buggenhout

Jenthe Van Buggenhout

19/08/2013 07:52:37
Quote Anchor link
Hallo,

Bedankt voor de info, ik heb de code al wat opgekuisd.
De afstand echoen gebeurd volgens mij vie de volgende code:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
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.
 
Reshad F

Reshad F

19/08/2013 12:23:34
Quote Anchor link
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.
 
Frank Nietbelangrijk

Frank Nietbelangrijk

19/08/2013 12:58:06
Quote Anchor link
Een mooiere oplossing is met PHP en CURL.

googledistance.class.php:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
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>


?>
Gewijzigd op 19/08/2013 13:00:21 door Frank Nietbelangrijk
 
Jenthe Van Buggenhout

Jenthe Van Buggenhout

23/08/2013 09:23:21
Quote Anchor link
Bedankt, het werkt volledig!!!
 
Frank Nietbelangrijk

Frank Nietbelangrijk

23/08/2013 09:48:04
Quote Anchor link
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)
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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);
    }

};


?>
 
Jenthe Van Buggenhout

Jenthe Van Buggenhout

23/08/2013 21:36:13
Quote Anchor link
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)

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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?
 
Local Dev

Local Dev

23/08/2013 22:37:24
Quote Anchor link
Beetje offtopic, maar please .. gebruik niet meer de oeroude en tevens al jarenlang niet meer onderhouden mysql functies, maar kijk naar PDO of MySQLi
 
Jenthe Van Buggenhout

Jenthe Van Buggenhout

23/08/2013 22:52:00
Quote Anchor link
Wat moet ik dan gebruiken?
thx voor de hulp
 
Local Dev

Local Dev

23/08/2013 23:04:41
Quote Anchor link
PDO of anders MySQLi
 
Frank Nietbelangrijk

Frank Nietbelangrijk

23/08/2013 23:47:47
Quote Anchor link
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo ($gd->getDistance())/1000 ?>


zal wel moeten worden:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php echo $gd->getDistance()/1000; ?>
 
Jenthe Van Buggenhout

Jenthe Van Buggenhout

24/08/2013 09:48:47
Quote Anchor link
Aangepast, kan er iemand mij nog verder helpen met de code of een voorbeeld van de code voor een INSERT uit te voeren in de db?

Bedankt, en al zeker bedankt voor de hulp ik leer hier enorm veel :)
 
Frank Nietbelangrijk

Frank Nietbelangrijk

24/08/2013 12:36:50
Quote Anchor link
wat heb je al in de database aan tabellen en kolommen?
 
Jenthe Van Buggenhout

Jenthe Van Buggenhout

24/08/2013 16:09:20
Quote Anchor link
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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 )
 
Frank Nietbelangrijk

Frank Nietbelangrijk

24/08/2013 16:33:56
Quote Anchor link
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
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);

?>
Gewijzigd op 24/08/2013 16:43:08 door Frank Nietbelangrijk
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.