【问题标题】:Geocode get latitude and longitude from google map api using curl in php [duplicate]地理编码使用php中的curl从谷歌地图api获取纬度和经度[重复]
【发布时间】:2015-04-03 20:52:12
【问题描述】:

我需要通过地址从 google map api 获取纬度和经度值。我使用 url "http://maps.googleapis.com/maps/api/geocode/json?address=".$doctorAddress."&sensor=true" 来从谷歌地图 api 中获取价值。我正在尝试使用 for 循环获取值列表。但我的问题是它只返回空值。我无法从 API 中获取价值。有没有办法得到它。谁能帮我做这件事。

【问题讨论】:

    标签: php google-maps curl


    【解决方案1】:

    以下是在 php 中使用 curl 从谷歌地图 api 获取纬度和经度的代码

    $address = "India+Panchkula";
    $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=India";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response);
    echo $lat = $response_a->results[0]->geometry->location->lat;
    echo "<br />";
    echo $long = $response_a->results[0]->geometry->location->lng;
    

    你也可以用下面的方法达到同样的效果

    $address = str_replace(" ", "+", $address);
    
    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
    $json = json_decode($json);
    
    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    

    【讨论】:

    • 是的,我知道。我做了相同的代码,但无法获得价值。它应该只返回空值。
    • @sankar 你应该已经发布了有问题的代码你现在可以发布它,因为上面的代码对我有用
    • @sankar 在您发表评论后,我再次对其进行了测试,它给了我输出 30.6942091 76.860565。只是为了确认是否启用了 curl?
    • 好吧好吧。我收到以下错误致命错误:超过 300 秒的最大执行时间
    • 我使用了 file_get_contents... 现在出现致命错误:php 中的最大执行时间超过了 300 秒
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2016-04-03
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多