Afstandsberekening postcodes

Pagina: 1
Acties:
  • 1.349 views

  • robbvdb
  • Registratie: Juni 2008
  • Laatst online: 22-09 23:20
Voor een mini-projectje zoek ik een oplossing om op basis van google-maps de afstand te kunnen berekenen tussen 2 postcodes. Het mooie is dat de API van google-maps hier de mogelijkheden voor geeft en dat je geen postcodetabellen hoeft aan te schaffen.

Nu heb ik al het één en ander gevonden als voorbeeld, maar door mijn veel te beperkte kennis van Java kom ik er zelf niet echt uit! Het volgende script dat ik vond werkt op zich prima maar ik krijg 2 dingen niet aangepast.

Het script rekent op basis van de gegevens die het van goolemaps terugkrijgt de afstand over de weg ( snelste route ) uit. Dit is op zich prima, maar.......ik zou graag de input `van ` (vertrekadres) vastzetten ( niet te veranderen door de gebruiker online) en alleen `naar ` invulbaar maken.

Verder zou ik graag de uitkomst verdubbeld zien, oftewel de kilometers op basis van heen én terugreis.

Als mooiste zou ik ook nog graag hieraan koppelen het aantal kilometers ( totaal heen en terug ) maal de kilometerprijs van € 0,19 zodat de kosten van de reis ook nog in beeld verschijnen.

Het voorbeeld dat ik vond is als volgt:

HTML:
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
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
<title>Connectcase.nl: Gratis afstand berekening tussen twee plaatsen met Google Maps</title>
<meta http-equiv="content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Gratis afstand berekening tussen twee plaatsen met Google Maps ipv. postcode tabel">
<meta name="keywords" content="afstand, berekening, kosten, twee, plaatsen, google maps, postcode, tabel, distance, calculation">
<meta name="abstract" content="Gratis afstand berekening tussen twee plaatsen met Google Maps ipv. postcode tabel">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="verify-v1" content="K8eGACCrm4Azib1o2Tk+4UQ9UJX1jk5WtQzsx4f8hVQ=" /> 
<meta name="y_key" content="b0c2a0e275c4c2b0" />
<meta name="revisit-after" content="7 Days" />
<meta name="ratings" content="general" />
<meta name="copyright" content="Connectcase © 2008">
<meta name="robots" content="all">
<meta name="language" content="nl">
<meta name="author" content="Connectcase.nl">
<meta name="copyright" content="Connectcase">
<meta http-equiv="Window-target" content="_top">

<script src="http://maps.google.nl/maps?file=api&amp;v=2&amp;key=ABQIAAAAEOe-MThhhCIOMKwWDPbwhRRS8CkXXPt5sVVf4YkaT1EeWYX3cBRwsjSrpuJm-goUrjhfsUYusXdklQ" 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/documentation/reference.html#GDirections
        gdir = new GDirections();
        GEvent.addListener(gdir,"load", set_distance); 
      }
    }

    function setDirections(fromAddress, toAddress, locale) 
    {
    // http://code.google.com/apis/maps/documentation/reference.html#GDirectionsOptions
    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().html; 
    }
    
  function handleErrors()
  {
  // http://code.google.com/apis/maps/documentation/reference.html#GGeoStatusCode
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY || gdir.getStatus().code == G_GEO_MISSING_ADDRESS)
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_UNAVAILABLE_ADDRESS)
     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_UNKNOWN_DIRECTIONS)
     alert("The GDirections object could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region.\n Error code: " + gdir.getStatus().code);     
   else if (gdir.getStatus().code == G_GEO_TOO_MANY_QUERIES)
     alert("The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time.\n Error code: " + gdir.getStatus().code);     
    }

  </script>

  </head>
<body onload="initialize()" onunload="GUnload()">
<form action="#" onsubmit="setDirections(this.from.value, this.to.value, 'nl'); return false">
<table border="0">
<tr>
<td>van:&nbsp;</td><td><input type="text" size="40" id="fromAddress" name="from" value="7327LB"/></td>
<td>naar:&nbsp;</td><td><input type="text" size="40" id="toAddress" name="to" /></td>
<td><input tabindex="1" name="submit" type="submit" value="Bereken afstand!" /></td>
</tr>
</table>
</form>
<div id="afstand"></div>
</body>
</html>


Na veel zoeken ook hier kom ik er niet uit. Iemand die me op weg helpt die de nodige ervaring heeft met Java?

[ Voor 0% gewijzigd door André op 12-02-2009 20:49 ]


  • Flipke84
  • Registratie: Juli 2008
  • Laatst online: 09-11-2024
Misschien zou je je TS eens kunnen verzien van code-tags dan wordt het geheel wat leesbaarder

  • Cyphax
  • Registratie: November 2000
  • Laatst online: 23:21

Cyphax

Moderator LNX
Als je er even rekening mee houdt dat de kans bestaat dat je zoektochten naar "Java" niet de juiste resultaten opleveren. Dat komt dan omdat je niet met Java bezig bent, maar met Javascript. :)
(niet om de wijsneus uit te hangen, maar het zoekt wat makkelijker als je de zoektermen niet verward met iets heel anders :))

Wat je in ieder geval kunt doen om 1 van die waarden niet invulbaar te maken is het formulier aanpassen:
code:
1
<form action="#" onsubmit="setDirections(this.from.value, this.to.value, 'nl'); return false">

Wat hier gebeurt is dat setDirections aangeroepen wordt, en de from en de to uit je html-formulier worden gehaald. Dus, als je daar bij this.from.value verandert in de vaste waarde ben je al een stuk verder. En dan het formulierveld uit je pagina halen. Alternatief is het invulveldje (<input type="text">) vervangen door een input van type "hidden", en een attribuutje "value" vullen met de postcode. Da's eigenlijk nog wel netter dan de eerste manier bij nader inzien.

[ Voor 53% gewijzigd door Cyphax op 12-02-2009 16:06 ]

Saved by the buoyancy of citrus


  • robbvdb
  • Registratie: Juni 2008
  • Laatst online: 22-09 23:20
Al doende leert men |:( , duurde even voordat ik het voor elkaar kreeg om de zaak wat leesbaarder te posten!

code:
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
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
<title>Connectcase.nl: Gratis afstand berekening tussen twee plaatsen met Google Maps</title>
<meta http-equiv="content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Gratis afstand berekening tussen twee plaatsen met Google Maps ipv. postcode tabel">
<meta name="keywords" content="afstand, berekening, kosten, twee, plaatsen, google maps, postcode, tabel, distance, calculation">
<meta name="abstract" content="Gratis afstand berekening tussen twee plaatsen met Google Maps ipv. postcode tabel">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="verify-v1" content="K8eGACCrm4Azib1o2Tk+4UQ9UJX1jk5WtQzsx4f8hVQ=" /> 
<meta name="y_key" content="b0c2a0e275c4c2b0" />
<meta name="revisit-after" content="7 Days" />
<meta name="ratings" content="general" />
<meta name="copyright" content="Connectcase © 2008">
<meta name="robots" content="all">
<meta name="language" content="nl">
<meta name="author" content="Connectcase.nl">
<meta name="copyright" content="Connectcase">
<meta http-equiv="Window-target" content="_top">

<script src="http://maps.google.nl/maps?file=api&amp;v=2&amp;key=ABQIAAAAEOe-MThhhCIOMKwWDPbwhRRS8CkXXPt5sVVf4YkaT1EeWYX3cBRwsjSrpuJm-goUrjhfsUYusXdklQ" 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/documentation/reference.html#GDirections
        gdir = new GDirections();
        GEvent.addListener(gdir,"load", set_distance); 
      }
    }

    function setDirections(fromAddress, toAddress, locale) 
    {
    // http://code.google.com/apis/maps/documentation/reference.html#GDirectionsOptions
    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().html; 
    }
    
  function handleErrors()
  {
  // http://code.google.com/apis/maps/documentation/reference.html#GGeoStatusCode
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY || gdir.getStatus().code == G_GEO_MISSING_ADDRESS)
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_UNAVAILABLE_ADDRESS)
     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_UNKNOWN_DIRECTIONS)
     alert("The GDirections object could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region.\n Error code: " + gdir.getStatus().code);     
   else if (gdir.getStatus().code == G_GEO_TOO_MANY_QUERIES)
     alert("The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time.\n Error code: " + gdir.getStatus().code);     
    }

  </script>

  </head>
<body onload="initialize()" onunload="GUnload()">
<form action="#" onsubmit="setDirections(this.from.value, this.to.value, 'nl'); return false">
<table border="0">
<tr>
<td>van:&nbsp;</td><td><input type="text" size="40" id="fromAddress" name="from" value="7327LB"/></td>
<td>naar:&nbsp;</td><td><input type="text" size="40" id="toAddress" name="to" /></td>
<td><input tabindex="1" name="submit" type="submit" value="Bereken afstand!" /></td>
</tr>
</table>
</form>
<div id="afstand"></div>
</body>
</html>


Cyphax alvast bedankt voor de snelle reply! Met een beetje spelen inderdaad heel eenvoudig de `van ` zichtbaar gezien eruit gehaald.

Door de afwezigheid van voldoende kennis van Javascript zal het vermenigvuldigen van de afstand x 2 en een berekening van de kilometerprijs nog wel een crime worden.

Ga in ieder geval nog eens flink zoeken (uiteraard op javascript ) maar sta natuurlijk heel erg open voor ideeën van hier!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

Door de afwezigheid van voldoende kennis van Javascript zal het vermenigvuldigen van de afstand x 2 en een berekening van de kilometerprijs nog wel een crime worden.
JavaScript:
1
var afstand = gdir.getDistance().meters * 2 / 1000;

8)7

Hoe dan ook, dit is hier dus niet de bedoeling. We zien hier niet bepaald graag dat je een lap code geeft, zegt hoe je het graag zou willen hebben en dan maar verwacht dat je het over een tijdje aangepast weer kan ophalen. Dat je niet goed bent in Javascript (let wel, da's zoals Cyphax al zegt niet hetzelfde als Java!) is daarbij geen excuus, aangezien je daar vast wel wat beginnerstutorials voor kan vinden. De rest is niet meer dan de documentatie van Google Maps erop naslaan om te kijken welke API-functies ze allemaal ondersteunen.

Stoei er nog even mee, en ga aan de slag met de tips uit dit topic. Als je verder nog problemen ondervindt kun je een nieuw topic openen, maar probeer je daarbij wel aan de quickstart te houden. En als je toch bezig bent: lees *** Over topictitels in PRG - lezen voor topic openen!!! *** ook even door. ;)

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Dit topic is gesloten.