我编写了一个具有以下代码的函数文件:
<?php
function get_lat_long_from_address($address)
{
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
return array($lat,$long);
}
//function get_closest_disco($client_adress)
//{
include ('connection.php');
$num_discos = get_num_discos();
$client_address = "C/ Sant Pere, 69, Barcelona";
list($client_lat,$client_long) = get_lat_long_from_address($client_address);
echo $client_address.'<br>Lat: '.$client_lat.'<br>Long: '.$client_long;
$query = 'SELECT adress FROM disco';
$result = mysql_query($query,$connect);
while ($row = mysql_fetch_row($result))
{
$disco_address[] = $row;
}
for ($i = 0; $i<count($disco_address); $i++)
{
$all_disco_address[] = implode(',', $disco_address[$i]);
}
echo '<br>';
var_dump($disco_address);
echo '<br>';
print_r($disco_address);
for ($i = 0; $i<count($disco_address); $i++)
{
list($disco_lat,$disco_long) = get_lat_long_from_address($disco_address[$i][0]);
/*$d = acos(sin($client_lat)*sin($disco_lat) +
cos($client_lat)*cos($disco_lat) *
cos($client_long-$disco_long)) * 6371000;*/
echo $all_disco_address[$i].'<br>Lat: '.$disco_lat.'<br>Long: '.$disco_long;
}
// }
?>
我想从我存储在数据库中的地址计算纬度和经度。然后我进行查询,并将结果存储在var $ disco_address []中。最后,我使用函数get_lat_long_from_address($ address)从这些地址获取纬度和经度。问题是,我可以从查询的第一个结果得到lat&amp; long,但不能用于以下那些...这是我得到的输出:
C/ Sant Pere, 69, Barcelona
Lat: 41.4451768
Long: 2.2451514
array(4) { [0]=> array(1) { [0]=> string(35) "C/ Nou de la Rambla, 113, Barcelona" } [1]=> array(1) { [0]=> string(29) "C/ Almogàvers, 122, Barcelona" } [2]=> array(1) { [0]=> string(22) "C/ A sobre de l'Api, 1" } [3]=> array(1) { [0]=> string(27) "C/ Serra i Moret, 6, Mataró" } }
Array ( [0] => Array ( [0] => C/ Nou de la Rambla, 113, Barcelona ) [1] => Array ( [0] => C/ Almogàvers, 122, Barcelona ) [2] => Array ( [0] => C/ A sobre de l'Api, 1 ) [3] => Array ( [0] => C/ Serra i Moret, 6, Mataró ) ) C/ Nou de la Rambla, 113, Barcelona
Lat: 41.3743451
Long: 2.1694939
Notice: Undefined offset: 0 in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Undefined offset: 0 in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
C/ Almogàvers, 122, Barcelona
Lat:
Long:
Notice: Undefined offset: 0 in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Undefined offset: 0 in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
C/ A sobre de l'Api, 1
Lat:
Long:
Notice: Undefined offset: 0 in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 33
Notice: Undefined offset: 0 in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\functions.php on line 34
C/ Serra i Moret, 6, Mataró
Lat:
Long:
看起来最后一个foor循环的第二次迭代有一个错误,带有函数get_lat_long_from_address($ address)的偏移量,但我不知道为什么。我很感激任何帮助。谢谢。
答案 0 :(得分:1)
问题出在这里
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
你必须像这样检查$output->status
:
if ($output->status == "OK") {
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
return array($lat,$long);
} else {
return array("error", $output->status);
}
并将其更改为:
list($client_lat,$client_long) = get_lat_long_from_address($client_address);
if ($client_lat == "error") {
echo "status error: " . $client_long;
exit;
}
答案 1 :(得分:0)
我想第33行和错误是$lat = $output->results[0]
。
在var_dump($output)
后尝试$output= json_decode($geocode);
jus查看从gmaps API返回的数据
无论如何,我试图以这种方式模仿您的代码:
$s = urlencode('C/ Sant Pere, 69, Barcelona');
$j = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$s.'&sensor=false');
$j = json_decode($j);
var_dump($j->results[0]);
我得到了正确的json结果,所以也许你需要在这里提供更多细节
答案 2 :(得分:0)
我终于找到了原因。是一个地址问题。看起来json_decode
不接受带重音的地址。我的数据库中的一些地址有像:Mataró,Almogàvers。哦,而且我的lenguage(加泰罗尼亚语)中街道的缩减根本不起作用(“C /”表示“Carrer”,只在某些情况下有效)。