login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Database tools > Geocacher

Geocacher

Auteur: BramBo - 15 augustus 2007 - 13:50 - Gekeurd door: Stijn - Hits: 4511 - Aantal punten: 4.50 (2 stemmen)




Als je net als mij een aantal grote adresbestanden hebt en geen zin om een vermogen uit te geven aan een coördinaten bestand (λ, φ) dan is dit de oplossing voor jou ;)
Het script werkt vrij straight to the point:
- Ophalen adressen bestand (vanuit je eigen database)
- Via Google maps Api (Geocoder) Latitude&Longitude ophalen
- Vast leggen in 'n array
- Posten dmv AJAX
- Array met gegevens opslaan in db

Alles wat je nodig hebt is:
- een MySQL database
- PHP (5)
- Google maps api key ( http://www.google.com/apis/maps/signup.html )
- Een flink adressen bestand ;)
- JSON Libaries; PHP&JS (aanwezig in het zip bestand)

(simpele) Voorbeeld database:
Table: adressen
| id | naam | straat | huisnr | woonplaats |

Table: Coords
| id | long | lati |
  1. CREATE TABLE `coords` (
  2. `id` int(8) NOT NULL default '0',
  3. `long` double NOT NULL default '0',
  4. `lati` double NOT NULL default '0',
  5. PRIMARY KEY (`id`)
  6. );
  7.  
  8. CREATE TABLE `coordsFailed` (
  9. `id` int(6) NOT NULL default '0',
  10. `attempts` tinyint(2) NOT NULL default '0',
  11. PRIMARY KEY (`id`)
  12. );


Hieronder staat de configuratie variabelen die te vinden zijn in het kopje van het script. Het enige verschil is dat ik hier wat extra commentaar bij heb gezet. (sorry dat alles in 't engels is zo werk ik nu eenmaal in mijn eigen bestanden)


Voordelen van 't script ? Simpel.. Snellere mapping met google. En natuurlijk alle wiskundige voordelen. Denk aan afstanden berekenen(voorbeeld volgt) Een scope berekenen en alles betrekken wat hier binnen valt(Ook hiervoor heb ik een script mocht je intresse hierin hebben laat t me even weten dan zullen we het erover hebben). And so on..

Afstanden tussen 2 coördinaten, maakt gebruik van de haversine formule ( http://en.wikipedia.org/wiki/Haversine_formula )
  1. print haversine(53.062605, 5.521006, 53.20298, 5.798562);
  2.  
  3. function haversine($lati1, $long1, $lati2, $long2) {
  4. $pi = M_PI / 180;
  5. $lati1 *= $pi;
  6. $long1 *= $pi;
  7. $lati2 *= $pi;
  8. $long2 *= $pi;
  9.  
  10. $a = sin(($lati2 - $lati1) / 2) * sin(($lati2 - $lati1) / 2) + cos($lati1) * cos($lati2) * sin(($long2 - $long1) / 2) * sin(($long2 - $long1) / 2);
  11. $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
  12.  
  13. return Round(6372.797 * $c, 2);
  14. }

Code:
  1. // Root Directory && Script
  2. define('root' , './'); //path to ur JSON.php include dir
  3. $script = 'index.php'; // Scriptname (of this script)
  4.  
  5. // See query below for explantion of these variables
  6. $limitStart = 0; // Where to start with fetching
  7. $limitTotal = 50; // Total amount of records to fetch
  8.  
  9. // Query to fetch addresses
  10. // The order needs to be as following :
  11. // ID -> Street -> StreetNumber -> City
  12. $qry = "SELECT id, straat, huisnr, woonplaats FROM adressen ORDER BY id ASC LIMIT {$limitStart}, {$limitTotal}";
  13. $addDB = preg_replace("/.*FROM ([\w\d_]+) ORDER.*/i","\\1", $qry);
  14.  
  15. // Query to save coords ( keep the #xxx# in it !)
  16. $save = "INSERT INTO coords VALUES(#ID#, #LONG#, #LAT#)";
  17.  
  18. // Query to save the FAILED coords ( keep the #xxx# in it !)
  19. $saveFail = "INSERT INTO coordsFailed VALUES(#ID#, ATTEMPTS+1)";
  20. $failDB = preg_replace("/.*INTO ([\w\d_]+) VALUES.*/i","\\1", $saveFail);
  21.  
  22. // Query to retry failed addresses
  23. $retry = "SELECT a.id, a.straat, a.huisnr, a.woonplaats, f.attempts FROM {$failDB} AS f JOIN {$addDB} AS a ON a.id=f.id";
  24.  
  25. // Google Maps
  26. $key = "KEY";
  27. $countryCode = "nl"; // nl = Netherlands; be= belguim; de = germany etc.
  28.  
  29. // Database
  30. $DBHost = "localhost";
  31. $DBUser = "USERNAME";
  32. $DBPass = "PASSWORD";
  33. $Dbase = "DATABASE";
  34. $cid = ""; // Empty, just initializing
  35.  
  36. // Visuals
  37. $barWidth = 200;
  38. $barColor = "00FF00"; // Without # !
  39. $contColor = "d9ff9d";
  40. $contBorder = "416900";
Download code! Download code (.txt)

Download dit script! Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (7)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.039s