dsa2c2255888 2012-02-17 20:57
浏览 34

google geolocator:直接URL工作,使用simplexml_load_file时状态代码620

I'm a little confused about this one.

I'm trying to lookup lats & lngs for a store locator and have been working with Google's Geolocator and their tutorial at http://code.google.com/apis/maps/articles/phpsqlgeocode.html, but can't seem to return anything other than the dreaded 620 Too many queries error.

What's really strange to me is that if I copy my $request_url into a browser I get all the XML as expected, but using PHP's simplexml_load_file($request_url) always returns error code 620, no matter how long of a delay, no matter how many different API keys I use.

Does anyone know what's up with this? Am I making some horrendous mistake?

My code is below, please excuse extensive comments and echoes:

<?php   

$host = 'xxx';
$db = 'xxx';
$u = 'xxx';
$p = 'xxx';

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

// Opens a connection to a MySQL server
$connection = mysql_connect($host, $u, $p);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($db, $connection);
if (!$db_selected) {
  die("Can\'t use db : " . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM BH_locations";
$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)) {   
    echo '<br /><br />row#' . $row['id'] . '<br />';

    $geocode_pending = true;

    while ($geocode_pending) {    
        echo 'started while loop at ' . date('G:i:s:u') . '<br />';
        $address = $row["address"] . ', ' . $row['city'] . ', ' . $row['state'] . ' ' . $row['zip'];  
        echo $address . '<br />';       

        $id = $row["id"];
        $request_url = $base_url . "&q=" . urlencode($address);      
        echo 'request url: ' . $request_url . '<br />';
        $xml = simplexml_load_file($request_url) or die("url not loading"); 
        echo '<pre>';
        var_dump($xml);
        echo '</pre>';

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

        if (strcmp($status, "200") == 0) {
            // Successful geocode
            echo '<br /><strong>WOOHOO IT WORKED!!!</strong><br />';
            $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 BH_locations " .
                " SET latitude = '%s', longitude = '%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;    
            echo 'error 620<br /><br />';
        } else {
            // failure to geocode
            $geocode_pending = false;
            echo "Address " . $address . " failed to geocoded. ";
            echo "Received status " . $status . "
";
        }

        usleep($delay);      

    } 

}     

?>
  • 写回答

1条回答 默认 最新

  • duanleiliu7373 2012-02-17 22:27
    关注

    You are sending too many requests too fast most likely.

    See G_GEO_TOO_MANY_QUERIES constant from the Google Maps documentation.

    The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time. If you're sending multiple requests in parallel or in a tight loop, use a timer or pause in your code to make sure you don't send the requests too quickly.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。