【发布时间】:2017-08-12 10:00:44
【问题描述】:
我正在尝试实现一项获取所提供地址的 GPS 坐标的服务,因此我想出了以下内容,该服务在本地运行良好。但是,当我尝试在托管服务提供商上实现它时,突然函数失败了,因为 Google 没有返回任何结果 - 甚至没有一个空白数组,什么都没有。
我认为它是通过网络主机阻止的,但不确定要查找什么。
功能:
function getCoordinates($Address, $APIKey) {
$BaseURL = "https://maps.googleapis.com/maps/api/geocode/json?key=".$APIKey."&address=";
$data = @file_get_contents($BaseURL.str_ireplace(" ","+",$Address));
//echo "Data:".$data."<br>";
$jsondata = json_decode($data,true);
//echo "<pre>";
//print_r($jsondata);
//echo "</pre>";
switch($jsondata["status"]) {
case "OK" :
if (count($jsondata["results"]) > 1) {
return array("status" => "FAIL", "message" => "Multiple results were returned");
}
else {
if (isset($jsondata["results"][0]["geometry"]["location"])) {
return array("status" => "SUCCESS","latitude" => $jsondata["results"][0]["geometry"]["location"]["lat"],"longitude" => $jsondata["results"][0]["geometry"]["location"]["lng"]);
}
}
return $jsondata["results"];
break;
case "ZERO_RESULTS" :
return array("status" => "FAIL", "message" => "Zero Results were returned");
break;
case "OVER_QUERY_LIMIT" :
return array("status" => "FAIL", "message" => "API Key is over the daily limit. It will automatically try again tomorrow");
break;
case "REQUEST_DENIED" :
return array("status" => "FAIL", "message" => "Request was denied");
break;
case "INVALID_REQUEST" :
return array("status" => "FAIL", "message" => "Invalid request, typically because the address is missing");
break;
case "UNKNOWN_ERROR" :
return array("status" => "FAIL", "message" => "Unknown error, Request could not be processed due to a Google server error. It may work again if you try later.");
break;
case "ERROR" :
return array("status" => "FAIL", "message" => "Error, the request timed out");
break;
default:
$Message = array("Failure",print_r($jsondata));
return array("status" => "FAIL", "message" => $Message);
break;
}
}
调用方式:
$LocationCoordinates = getCoordinates("123 Main Street, Toronto, ON", [[$MapsAPIKey]]);
出于测试目的,我取消了函数顶部附近的 4 行的注释,第一行返回了单词“Data”,后面没有任何内容。接下来的三行,预计只是返回了相应的'pre'标签。
检查了控制台日志,没有显示错误。我在服务器上尝试了一个快速的客户端脚本,它似乎工作正常。
【问题讨论】:
标签: php google-maps-api-3