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

url not loading

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 :(

Recommended Answers

All 7 Replies

Member Avatar for LastMitch

2 0.2067 283408 simplexml_load_file ( ) ..\geocode.php:57

The reason you have that error because strcmp() function is not reading it as a string.

Another words your xml is not loading in here:

$status = $xml->Response->Status->code;

Try to change this:

mysql_real_escape_string() 

to this:

mysqli_real_escape_string() 

since you are using mysqli

Then that will remove error for line 57.

Thank you! I changed but I still get the same error.
Here is the problem: I had to eliminate some blank lines I had in my code because daniweb.com did now alllow me to post the code as I had in my computer. Sorry about this. Line 57 is actually
$xml = simplexml_load_file($request_url) or die("url not loading"); Line 36 at daniweb (above).

Anyway, that IS a good point. I am starting with computers and I believe the computer or the interpreter would stop at that line. My guess is that it is stuck a little earlier (if a computer reads a code like us :/ from the top down.

Anyway, I fixed lines 49-52. (PHP changes, Google changes....).

These are the messages I have:
(1) Warning: simplexml_load_file(http://maps.googleapis.com/maps/api/geocode/output?parameters&q=4200+Park+Blvd+): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in C:\wamp\www\test\geocode.php on line 57 (read line 36 at daniweb.com)
(2) 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 (read line 36 at daniweb.com)
(3) url not loading
Line 57 (my computer) is line 36 above. My apologies again.

I do have a question about this xml file. Is it produced by the geocode? I am really new to this as you can see.... All the tutorials are deprecated and the book I read is only about map and all the good stuff on maps itself, not related to database.

Member Avatar for LastMitch

I had to eliminate some blank lines I had in my code because daniweb.com did now alllow me to post the code as I had in my computer.

Why? and what are you talking about?

So the error you are having is here:

$xml = simplexml_load_file($request_url) or die("url not loading"); Line 36 at daniweb (above).

Another words there's something wrong with this:

$request_url = $base_url . "&q=" . urlencode($address);

When you look closely there's something wrong with this:

$base_url = "http://maps.googleapis.com/maps/api/geocode/output?parameters";

The $base_url should look like this:

$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;

Since you DEFINE it here:

define("MAPS_HOST", "maps.google.com");
define("KEY", "AIzaSyBMUQ7TBXddGfNzQrEQ5VAEES8cbe_JSo8");

There shouldn't be any issue.

Next please highlight your error. It's hard to read it.

Thank you for your time and patience.

I tried that before and was getting the following error:
*error: Address 4200 Park Blvd failed to geocoded. Received status 610. *
Yes, I tried after your email again, same error.

This is what google says about 610:

*G_GEO_BAD_KEY = 610     The given key is either invalid or does not match the domain for which it was given.*

The map loads, it does not get lat and lnt (geocode). I am assuming the key is good.

There are several people complainning this month about the same problem, but nobody posted the solution.
In one of the forums, people raised the problems about this url and the transition between Google APi V2 to V3. Info here.

In other places, people are saying that Google Maps V3 does not need the key anymore because the code is not requering it anymore (probably google is using our IP??).

This is supposed to be the new url for V3.

$base_url = "http://maps.googleapis.com/maps/api/geocode/output?parameters"

The other places people even said they had stable web pages and suddenly stopped working and getting the 610 error message (with the code using the key). I kept the new code with no key because that is supposed to be the new code for Google maps V3. The question is that I don't know if they solved the problem though this change. I asked, no answer so far....

Member Avatar for LastMitch

In other places, people are saying that Google Maps V3 does not need the key anymore because the code is not requering it anymore (probably google is using our IP??).

What version are you using?

Then why you have this:

define("KEY", "AIzaSyBMUQ7TBXddGfNzQrEQ5VAEES8cbe_JSo8");

They have a full tutorial for the V2. Supposed to be fully deprecated in May or Septemember (depends on the google page you get the info from).
Then they have another update for the V3 (which is actually info about upgrading from V2 to V3). However the new version does not have a tutorial for newbies. Besides it's not meant to be a code for pulling address from database, getting geocode, returning to database with lag and lnt, send it to google maps.... It's pretty complicated stuff. The new version (3) brings the changes (the url is one of the them and therefore I changed it that). Here it is.
I removed the info about the key, the errors are the same.Thank you for your your help anyway!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.