下面是我当前使用的代码,我在其中将地址传递给函数,并且 Nominatim API 应该返回一个 JSON,我可以从中检索地址的纬度和经度。
function geocode($address){
// url encode the address
$address = urlencode($address);
$url = 'http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$address}&format=json&limit=1';
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// get the important data
$lati = $resp['lat'];
$longi = $resp['lon'];
// put the data in the array
$data_arr = array();
array_push(
$data_arr,
$lati,
$longi
);
return $data_arr;
}
它的问题是我总是以内部服务器错误告终。我检查了日志,这不断重复:
[[DATE] America/New_York] PHP Notice: Undefined index: title in [...php] on line [...]
[[DATE] America/New_York] PHP Notice: Undefined variable: area in [...php] on line [...]
这里可能是什么问题?是因为_
在New_York
吗?我曾尝试将str_replace
其与 a 交换,+
但这似乎不起作用,并且仍然返回相同的错误。
此外,该 URL 工作正常,因为我已通过 JavaScript 和手动对其进行了测试(尽管{$address}
已替换为实际地址)。
非常感谢您对此的任何帮助,谢谢!
编辑
现在已解决此问题。问题似乎是 Nominatim 无法获取某些值,因此返回错误