Reputation: 4137
I have write a function file that has de following code:
<?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;
}
// }
?>
I want to calculate the latitude and longitude from the adresses I'have stored in my db. Then I make my query and I store the results on the var $disco_address[]. Finally I the function get_lat_long_from_address($address) to obtain the latitude and the longitude from those addresses. The problem is that I can get the lat&long from the first result of the query, but not for the following ones... This i the output I get:
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:
Looks like there's an error from the second iteration of the last foor loop with the offset of the function get_lat_long_from_address($address), but I don't know why. I'd appreciate any help. Thank you.
Upvotes: 0
Views: 696
Reputation: 4137
I finally found why. Was a matter of adresses. Looks like json_decode
doesn't accept addresses with accents. Some of my addresses in my db had accents like: Mataró, Almogàvers. Oh, and also the abreviation of street in my lenguage (catalan) doesn't work well at all ("C/" means "Carrer" and only work in some cases).
Upvotes: 0
Reputation: 3695
I suppose the line 33 and the error is $lat = $output->results[0]
.
Try to var_dump($output)
jus after $output= json_decode($geocode);
to see the data returned from gmaps API
Anyway I'm trying to emulate your code this way:
$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]);
and I get the correct json result, so maybe you need to provide more details here
Upvotes: 0
Reputation: 5092
The problem is here
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
You have to check the $output->status
like this:
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);
}
And change this to:
list($client_lat,$client_long) = get_lat_long_from_address($client_address);
if ($client_lat == "error") {
echo "status error: " . $client_long;
exit;
}
Upvotes: 1