0

Until recently I've been using google geocoding to provide coordinates to be saved in a database. However, when I recently tried to do this it failed, with error 610. I was still using v2 and I understand this is phasing out. So, I came on this website and had a look at this thread: Changing from Google maps api v2 to v3. I updated my code in line with the following (which I understand from the feedback worked) from this thread:

These are, changing the address for v3 geocoding

define("MAPS_HOST", "maps.googleapis.com");
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?";

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

And secondly changing the path in the returned xml file for setting lat/long

    $coordinates = $xml->Response->Placemark->Point->coordinates;
    $coordinatesSplit = split(",", $coordinates);
    // Format: Longitude, Latitude, Altitude

    $lat = $coordinatesSplit[1];

    $lng = $coordinatesSplit[0];

Can be completely replaced with

    $lat = $xml->result->geometry->location->lat;
    $lng = $xml->result->geometry->location->lng;

But still not working for me. I no longer get error 610 when I try to run it, just "failed to geocode, error code ".

Apologies I'm still relatively novice with this stuff, learning as I go really, so I appreciate any help you can give. It may be the simplest thing I'm missing. Here's my code currently:

    <?php
    require("phpsqlgeocode_dbinfo.php");

    define("MAPS_HOST", "maps.googleapis.com");
    define("KEY", *my key*);

    // Opens a connection to a MySQL server
    $connection = mysql_connect('*name*', $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 1";
    $result = mysql_query($query);
    if (!$result) {
      die("Invalid query: " . mysql_error());
    }

    // Initialize delay in geocode speed
    $delay = 0;
    $base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?";

    // Iterate through the rows, geocoding each address
    while ($row = @mysql_fetch_assoc($result)) {
      $geocode_pending = true;

      while ($geocode_pending) {
        $address = $row["address"];
        $id = $row["id"];
        $request_url = $base_url . "address=" . urlencode($address) . "&sensor=false";
$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;
  $lat = $xml->result->geometry->location->lat;
  $lng = $xml->result->geometry->location->lng;

  $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 "Address " . $address . " failed to geocoded. ";
  echo "Received status " . $status . "
    \n";
       }
        usleep($delay);
      }
    }
    ?>

Thanks so much!

Community
  • 1
  • 1
  • possible duplicate of [Google geocoding using v3 not showing error](http://stackoverflow.com/questions/16762433/google-geocoding-using-v3-not-showing-error) – Dr.Molle May 30 '13 at 23:46

1 Answers1

0

This is what I did. I was having the same problem and now everything works perfectly.

<?php 

      $sql = "SELECT * FROM locations WHERE lat IS NULL AND lng IS NULL";
            $res = mysql_query( $sql );
            while( $row = mysql_fetch_assoc( $res ) ) {
                $address = $row['address_1'] . ", " . $row['city'] . " " . $row['state'] . " " . $row['zip'];
                $latlng = geocode( $address );
                echo $address . "<br />";
                if( $latlng[0] && $latlng[1] ) {
                    print_r( $latlng );
                    echo "<br />";
                    $update = "UPDATE locations SET lat = '" . $latlng[0] . "', lng = '" . $latlng[1] . "' WHERE id = " . $row['id'] . " LIMIT 1";
                    $update_res = mysql_query( $update );
                    echo $update . "<br />";
                } else {
                    echo $latlng[2] . "<br />";
                }
                echo  "<br />";
            }

function geocode( $address ) {
    if( empty( $address ) ) {
        return array("", "", "");
    }

    $base_url = "http://maps.googleapis.com/maps/api/geocode/xml";
    $request_url = $base_url . "?address=" . urlencode( $address ) . "&sensor=false" ;
    $xml = simplexml_load_file( $request_url );
    $status = $xml->status;
    if (strcmp($status, "OK") == 0)  {

        $lat = $xml->result->geometry->location->lat;
        $lng = $xml->result->geometry->location->lng;
        return array( $lat, $lng, $request_url );
    } else {
        return array("", "", $request_url);
    }
}

?>
Octahedron
  • 893
  • 2
  • 16
  • 31
David
  • 1