I am designing my very own first web page. It's a real estate page where some users will add open house information while others can pull the info out.
The code for the database and the web is very good (after two months...) and inserting and getting data is sweet.
Now the Google maps.... I am trying to have something like trulia or even the very original housingmaps.com (It seems that they started the mash up, right?). crimereports.com is very nice, and a better example. Crime map updates google map as you drag it and creates a nice pop table (drop down menu upper right) with the crimes you see in current window.
GOAL: pull addresses from database, get long and lat from geocode, insert it back to table, display in google map. Plus: as people pan the map, new info pops into map.
Here is the code for retrieving addresses, geo code, and add lat and lng back to database.
<?php
//require("phpsqlajax_dbinfo.php");
// Opens a connection to a MySQL server
$username = "aaa";
$password = "aaa";
$hostname = "aaa";
$database = "aaa";
$connection = mysqli_connect($hostname, $username, $password);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
define("MAPS_HOST", "maps.google.com");
define("KEY", "AIzaSyBMUQ7TBXddGfNzQrEQ5VAEES8cbe_JSo8");
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM brokerstour.property WHERE 1";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://maps.googleapis.com/maps/api/geocode/output?parameters";
// Iterate through the rows, geocoding each address
while ($row = @mysqli_fetch_assoc($result)) {
$geocode_pending = true;
while ($geocode_pending) {
$address = $row["address"];
$id = $row["id"];
$request_url = $base_url . "&q=" . urlencode($address);
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
$query = sprintf("UPDATE property " .
" 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 "Address " . $address . " failed to geocoded. ";
echo "Received status " . $status . "
\n";
}
usleep($delay);
}
}
?>
Here is what I have changed in the Google Tutorial (original code above):
1) updated php: mysqli to avoid errors. Not getting more php errors.
2) changed the url from "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
TO
"http://maps.googleapis.com/maps/api/geocode/output?parameters"
trying to update code to API v3.
If I use old url, i get error 610.
With new url I get (those orange box error messages):
------------------
( ! ) SCREAM: Error suppression ignored for
( ! ) Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://maps.googleapis.com/maps/api/geocode/output?parameters&q=4200+Park+Blvd+" in C:\wamp\www\test\geocode.php on line 57
Call Stack
2 0.2067 283408 simplexml_load_file ( ) ..\geocode.php:57
Full disclosure: I am very new and maybe the above doesn't even make sense. Maybe I am trying to go north and heading south and asking for directions ... However I have been stuck on this problem for a week. (By the way for those new to google maps, the book Beginning Google API 3 is a must. It does not deal with database though, but explains the google maps api very well).
Could someone please give any a hint, book, tutorial? Any help is a great help.
Thank you for your time.
PS. Maybe the old tutorial for google v2 no longer applies and the url change is not enough :(