【发布时间】:2017-06-07 22:28:59
【问题描述】:
下面是我当前使用的代码,我在其中将地址传递给函数,并且 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 将其与+ 交换,但这似乎不起作用并且仍然返回相同的错误。
此外,该 URL 工作正常,因为我已通过 JavaScript 手动对其进行了测试(尽管 {$address} 已替换为实际地址)。
非常感谢您对此的任何帮助,谢谢!
编辑
此问题现已修复。问题似乎在于 Nominatim 无法获取某些值,因此返回错误
【问题讨论】:
标签: php json geocoding latitude-longitude nominatim