调用Geocode很好,但现在有时很慢

时间:2013-06-12 21:14:07

标签: php google-api geocoding google-geocoder

我几个月来一直使用谷歌地理编码呼叫没有问题。 直到几天前:现在它有时需要60秒才能回复 -  状态为“OK”,我需要的字段出现在返回区域中。

大部分时间都很好,但我偶尔会遇到问题。 我通过快速连续拨打3或4个电话重新创建了问题: 最后一次通话需要60秒以上。

我已确认:

  • 延迟发生在curl_exec($ch)声明中。 (我通过运行代码来完成此操作,但不包括此语句,没有问题。一旦我包含curl_exec($ch),那么在几次调用之后,它将花费超过60秒。)

    < / LI>
  • 我无法接近每天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);

我非常感谢任何帮助。

由于 布赖恩。

0 个答案:

没有答案