我根据Android应用程序向我的服务器发出的请求,使用以下代码获取城市坐标。:
function getGPSLocation($reqLocation) {
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $reqLocation . "&sensor=false";
$attempt = 0;
$this->log($url, 'debug' );
$location = [
'lat' => 0.0,
'lng' => 0.0
];
while ($attempt < 3) {
$timeout = 5;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
$resultHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($resultHttpCode == '200') {
$result = json_decode($content);
if ($result->status == 'OVER_QUERY_LIMIT') {
$this->log($result->status, 'debug' );
sleep(2);
$attempt ++;
} else {
/*
* TODO: Store the result to prevent other similar request later.
*/
if ($result != null) {
$location = $result->results[0]->geometry->location;
}
break;
}
}
}
return $location;
}
此代码完全适用于我的本地服务器,但是当我从在线服务器执行任何请求时,我收到OVER_QUERY_LIMIT错误。
Google文档(https://developers.google.com/maps/documentation/business/articles/usage_limits),并没有告诉我应该解释这种行为的任何内容。
有什么想法吗?
答案 0 :(得分:0)
在您对网络服务的请求中使用密钥,因此谷歌可以将您的请求与我(假设)共享服务器中的任何其他请求分开。
来自documentation 的
API密钥
注意:Maps for Work用户必须在其请求中包含客户端和签名参数,而不是密钥。
所有地理编码API应用程序都应使用API密钥。在您的请求中包含密钥:
允许您在Google Developers Console中监控应用程序的API使用情况。 启用每个密钥而不是每个IP地址配额限制。 确保Google可以在必要时就您的申请与您联系。 Geocoding API使用API密钥来标识您的应用程序。 API密钥通过Google API控制台进行管理。要创建密钥:
访问Google Developers Console中的API控制台,然后使用您的Google帐户登录。 单击API控制台左侧菜单中的“服务”链接,然后激活地理编码API服务。 激活服务后,您的API密钥可从API&gt;获得。访问页面,在Simple API Access部分中。地理编码API应用程序使用Key for servers apps。 要在请求中指定密钥,请将其包含为密钥参数的值。
注意:默认情况下,可以从任何服务器使用密钥。我们强烈建议您通过IP地址将密钥的使用限制为您管理的服务器。您可以通过单击API控制台中的Edit allowed referers ...链接来指定允许使用API密钥的IP地址。
注意:对包含API密钥的请求强制执行HTTPS。