几个月来我一直在使用 Google Geocode 调用,没有出现任何问题。直到几天前:现在有时需要超过 60 秒才能响应 - 状态为“OK”并且我需要的字段出现在返回区域中。
大部分时间都很好,但我偶尔会遇到问题。我通过快速连续拨打 3 或 4 次电话重新造成了问题:最后一次通话需要 60 多秒。
我已经确认:
即延迟发生在
curl_exec($ch)
声明中。(我通过运行代码来做到这一点,但不包括这个语句,没有问题。只要我包含curl_exec($ch)
then,经过几次调用,它需要超过 60 秒)。我离每天 2,500 个请求的限制还差得很远。
状态字段是“OK”而不是“OVER_QUERY_LIMIT”。
我正在使用 PHP(版本:5.2.17)。
代码如下:
function geocode($postcode){
$locality = '';
$administrative_area_level_2 = '';
$administrative_area_level_1 = '';
$postal_town = '';
$formatted_address = '';
$longitude = 0 ;
$latitude = 0 ;
$postcode1 = str_replace(' ','_',$postcode) . ',UK';
$string = str_replace (" ", "+", urlencode($string));
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$postcode1."&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$temp = curl_exec($ch) ;
$response = json_decode($temp, true);
$geo_error = $response['status'];
if ( $geo_error == 'OK' ) { // oooooooooooooooooooooooooooooo
$names = array();
$names[0] = $response['results'][0]['address_components'][0][long_name] ;
$names[1] = $response['results'][0]['address_components'][1][long_name] ;
$names[2] = $response['results'][0]['address_components'][2][long_name] ;
$names[3] = $response['results'][0]['address_components'][3][long_name] ;
$names[4] = $response['results'][0]['address_components'][4][long_name] ;
$types = array();
$types[0] = $response['results'][0]['address_components'][0][types][0] ;
$types[1] = $response['results'][0]['address_components'][1][types][0] ;
$types[2] = $response['results'][0]['address_components'][2][types][0] ;
$types[3] = $response['results'][0]['address_components'][3][types][0] ;
$types[4] = $response['results'][0]['address_components'][4][types][0] ;
for ($i = 0; $i <= 4; $i++) {
switch ($types[$i]) {
case 'locality':
$locality = $names[$i];
break;
case 'administrative_area_level_2':
$administrative_area_level_2 = $names[$i];
break;
case 'administrative_area_level_1':
$administrative_area_level_1 = $names[$i];
break;
case 'postal_town':
$postal_town = $names[$i];
break;
default:
break;
} // end of switch
} // end of for
$formatted_address = $response['results'][0]['formatted_address'] ;
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lng'];
$latitude = $geometry['location']['lat'];
$address_components = $response['results'][0]['address_components'];
$array = array(
'latitude' => $geometry['location']['lat'],
'longitude' => $geometry['location']['lng'],
'location_type' => $geometry['location_type'],
);
} else { // ooooooooooooooooooooooooooooooooooooooooooooo
// geo did not work so log error and report
} // end if oooooooooooooooooooooooooooooooooooooooooooo
return array($longitude,$latitude,$locality,$administrative_area_level_2,
$administrative_area_level_1,$postal_town,$formatted_address,$geo_error);
我将非常感谢任何帮助。
谢谢布赖恩。