我尝试使用建议的答案:Get latitude and longitude automatically using php, API
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=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
答案 0 :(得分:0)
做这样的事情
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 ";
}
答案 1 :(得分:0)
我在PHP 5.5.11机器上测试了你的代码并且它可以工作。
尝试在error_reporting(E_ALL);
之后直接添加<?php
。
您确定已启用curl模块吗?