[GIS] Google geocoding v3 stopped working

geocodinggooglegoogle-maps-api

I've been using Google geocoding v3 for about 6 months but all of a sudden it's stopped working (I get a 610 error). It's only stopped in the last week or so.

Then I came across this (see the pink box at the top of the page!): https://developers.google.com/maps/documentation/geocoding/v2/

I've read through all the documentation and not sure where to start!

I'm hoping it's a small change as it's taken a long time to get this far, can anyone help?

See the full site here

This is my code:

<script src="<?php bloginfo('template_directory'); ?>/markerclusterer.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

This is doing the geocoding:

<?php 

require("database.php");
// Opens a connection to a MySQL server
$con = mysql_connect("localhost", $username, $password);

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("medicom_wp", $con);


    $company = get_the_title();
    $address = get_field('address_line_1');
    $city = get_field('town_/_city');
    $post_code = get_field('post_code');
    $link = get_permalink();
    $type = get_field('kind_of_organisation');

    $sql = sprintf("select count('x') as cnt from markers where `name` = '%s'", mysql_real_escape_string($company));
    $row_dup = mysql_fetch_assoc(mysql_query($sql,$con));
    if ($row_dup['cnt'] == 0) {
        mysql_query("INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`, `link`) VALUES ('".$company."', '".$address.", ".$city.", ".$post_code."', '0.0', '0.0', '".$type."', '".$link."')");
}
wp_reset_query();


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

// Opens a connection to a MySQL server
$connection = mysql_connect("localhost", $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/geo?output=xml" . "&Key=" . KEY;

// 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 . "&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 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 += 1000;
    } else {
      // failure to geocode
      $geocode_pending = false;
      echo "Address " . $address . " failed to geocoded. ";
      echo "Received status " . $status . "
\n";
    }
    usleep($delay);
  }
}


?>

This is creating the xml:

<?php
require("database.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $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());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'link="' . $row['link'] . '" ';  
  echo 'type="' . $row['type'] . '" ';
  echo '/>';
}
// End XML file
echo '</markers>';
?>

This is generating the map:

   <script type="text/javascript">
    //<![CDATA[

    var customIcons = {
      Academic: {
        icon: 'http://www.teamworksdesign.com/clients/poh/wp-content/themes/default/images/pin-academic.png',
      },
      Business: {
        icon: 'http://www.teamworksdesign.com/clients/poh/wp-content/themes/default/images/pin-business.png',
      },
      Clinical: {
        icon: 'http://www.teamworksdesign.com/clients/poh/wp-content/themes/default/images/pin-clinical.png',
      },
      Government: {
        icon: 'http://www.teamworksdesign.com/clients/poh/wp-content/themes/default/images/pin-government.png',
      },  
      Charity: {
        icon: 'http://www.teamworksdesign.com/clients/poh/wp-content/themes/default/images/pin-charity.png',
      }                     
    };

    function load() {
      var cluster = [];
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(52.375599, -3.471680),
        zoom: 7,
        mapTypeId: 'roadmap'
      });
      var infowindow = new google.maps.InfoWindow();
      var min = .999999;
      var max = 1.000002;

      // Change this depending on the name of your PHP file
      downloadUrl("<?php bloginfo('stylesheet_directory'); ?>/phpsqlajax_genxml.php ", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var type = markers[i].getAttribute("type");

          var offsetLat = markers[i].getAttribute("lat") * (Math.random() * (max - min) + min);
          var offsetLng = markers[i].getAttribute("lng") * (Math.random() * (max - min) + min);

          var point = new google.maps.LatLng(offsetLat, offsetLng);
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow
          });
          google.maps.event.addListener(marker, 'click', (function(marker, i) {
                        return function() {
                            var name = markers[i].getAttribute("name");
                            var address = markers[i].getAttribute("address");
                            var type = markers[i].getAttribute("type");
                            var link = markers[i].getAttribute("link");
                            var html = "<b>" + name + "</b> <br/>" + type + "<br/><br/><a href='" + link + "'>View profile</a>";
                            infowindow.setContent(html);
                            infowindow.open(map, marker, html);
                            // infowindow.setContent(html);
                            // infowindow.open(map, marker);
                        }
                    })(marker, i));
          cluster.push(marker);
        }
        var mc = new MarkerClusterer(map,cluster);
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}

    //]]>

Best Answer

610 = G_GEO_BAD_KEY

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

https://developers.google.com/maps/documentation/javascript/v2/reference#GGeoStatusCode

Check with IT/ISP that the IP has not changed (request a Static IP for your IT/ISP).

You can request another API Key (check on your dev/test server first)

Temporary workaround V3 does NOT require an API key (though sensor=*true_or_false*) is

example: http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

You will be limited to 1500 geocodes requests per 24 hours.

https://developers.google.com/maps/documentation/geocoding/