【问题标题】:How to use Nominatim API through PHP to retrieve latitude and longitude?如何通过 PHP 使用 Nominatim API 来检索经纬度?
【发布时间】: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


    【解决方案1】:

    鉴于变量titlearea 不存在,您提到的错误似乎与您发布的代码无关。我可以为您发布的geocode 功能提供一些帮助。

    主要问题是$url 字符串周围有单引号 - 这意味着$address 没有注入到字符串中,并且请求是针对“$address”的纬度/经度。使用双引号可以解决这个问题:

    $url = "http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$address}&format=json&limit=1";
    

    其次,响应包含一个数组数组(如果不是limit 参数,可能会出现多个结果)。因此,当从响应中获取详细信息时,请查看 $resp[0] 而不仅仅是 $resp

    // get the important data
    $lati = $resp[0]['lat'];
    $longi = $resp[0]['lon'];
    

    完整,为简单起见,在末尾添加了一些数组构建的缩写:

    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);
    
        return array($resp[0]['lat'], $resp[0]['lon']);
    
    }
    

    如果您对它的工作感到满意,我建议您为 http 请求和响应的解码/返回添加一些错误处理。

    【讨论】:

    • 感谢您的回复!出于某种原因,我仍然收到服务器错误,并且错误日志说同样的东西。该文件本身可以正常工作,因为它可以在没有该功能的情况下执行。
    • @Osman American/New_York 部分是日志时间戳的时区。你能在你使用titlearea的地方发帖吗?
    • 我已更新问题详细信息,我正在使用 Slack API 检索有关用户团队的数据。该代码在我使用 Google 的地理编码器时有效,但在切换到 Nominatim 时发生错误
    • 对,所以这些错误可能不相关。试试enabling error displaying 看看你是否可以显示实际问题。
    • 好的,这似乎是 Nominatim 本身的问题。我已经测试了 [nominatim.openstreetmap.org] 上的地址,它们不返回值。它适用于谷歌地图地理编码器
    猜你喜欢
    • 2014-11-15
    • 2015-03-12
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2014-10-19
    相关资源
    最近更新 更多