【问题标题】:latitude and longitude automatically using php, API经纬度自动使用php,API
【发布时间】:2014-10-19 10:06:19
【问题描述】:

我尝试使用建议的答案:Get latitude and longitude automatically using php, 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;

但我总是遇到 3 个错误

未定义的偏移量:0 并试图获取非对象的属性

这些错误对应于存在的行:

$lat = $response_a->results[0]->geometry->location->lat;
$long = $response_a->results[0]->geometry->location->lng;

有没有人经历过这些?我正在使用 php 5.5

【问题讨论】:

  • 你确定这是唯一涉及的代码吗?作品fine

标签: php google-maps-api-3


【解决方案1】:

做这样的事情

if($response!="")
{
    $response_a = json_decode($response);
    if(isset($response_a->results[0]))
    {
        echo $lat = $response_a->results[0]->geometry->location->lat;
        echo "<br />";
        echo $long = $response_a->results[0]->geometry->location->lng;
    }
    else
    { 
        echo "result is not set in response";
    }   
}
else
{
    echo "No respnse ";
}

【讨论】:

  • 感谢 SkRocks 我已经尝试了您的代码,我得到以下信息:结果未设置为响应。这是 json_decode 的问题吗?你可以查看我的地址awil.ch/test_curl.php
  • 不,这意味着给定地址没有得到任何结果
【解决方案2】:

我在 PHP 5.5.11 机器上测试了您的代码,它可以工作。

尝试在&lt;?php之后直接添加error_reporting(E_ALL);

您确定 curl 模块已启用吗?

【讨论】:

  • 感谢您的回答我已添加 error_reporting(E_ALL);但它没有改变任何东西,我如何检查 curl 模块是否启用?当我这样做时: echo function_exists('curl_version');我得到 1
  • 试试这个:var_dump(extension_loaded('curl')); 注意,对 Google GeoCoder 的请求是有限的。尝试在浏览器中打开网址:maps.google.com/maps/api/geocode/…
  • Alexander with var_dum 我得到了TRUE,你可以看到运行代码@awil.ch/test_curl.php
  • 也许这与谷歌的限制有关,但我怀疑我是否达到了每 24 小时 2,500 个请求甚至每秒 10 个请求。你知道是否有办法知道你的极限水平吗?我在谷歌上寻找类似的东西,但确实找到了。
  • 我在awil.ch/test_curl.php 上调用了您的代码并获得了职位。查看屏幕:i58.tinypic.com/1ep3qo.png
猜你喜欢
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
相关资源
最近更新 更多