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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
<?php
require '....dbinfo.php';
define("MAPS_HOST", "maps.google.com");
define("KEY", "PUT YOUR KEY HERE");
$nrID=$_GET["nrID"];
// Opens a connection to a MySQL server
$connection = mysql_connect($host, $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE id=".$nrID;
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=csv&key=" . KEY;
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Company</th>
<th>Address</th>
<th>Zip</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Status</th>
</tr>";
// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
$geocode_pending = true;
while ($geocode_pending) {
$company = $row["name"];
$address = $row["address"] . "," . $row["zip"] . "," . $row["city"] . "," . $row["state"] . "," . $row["country"];
$id = $row["id"];
$request_url = $base_url . "&q=" . urlencode($address);
$csv = file_get_contents($request_url) or die("url not loading");
$csvSplit = split(",", $csv);
$status = $csvSplit[0];
$lat = $csvSplit[2];
$lng = $csvSplit[3];
if (strcmp($status, "200") == 0) {
// successful geocode
$geocode_pending = false;
$lat = $csvSplit[2];
$lng = $csvSplit[3];
//echo "The company " . $company. " " . $address . " was correctly geocoded. ";
//echo " ----> Received lat and long = " . $lat . ";" . $lng . "\n <br>";
$query = sprintf("UPDATE markers " .
" SET lat = '%s', lng = '%s' " .
" WHERE id = %s LIMIT 1;",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($id));
$update_result = mysql_query($query);
if (!$update_result) {
die("Invalid query: " . mysql_error());
}
} else if (strcmp($status, "620") == 0) {
// sent geocodes too fast
$delay += 100000;
}
else {
// failure to geocode
$geocode_pending = false;
echo "<tr>";
echo "<td>" . $row['id'] . " </td>";
echo "<td>" . $row['name'] . " </td>";
echo "<td>" . $row['address'] . " </td>";
echo "<td>" . $row['zip'] . " </td>";
echo "<td>" . $row['city'] . " </td>";
echo "<td>" . $row['state'] . "TEST </td>";
echo "<td>" . $row['country'] . " </td>";
echo "<td>" . $status . " = failed to geocoded</td>";
echo "</tr>";
}
}
usleep($delay);
}
echo "</table>";
?> |
Partager