This is my PHP code to find latitude and longitude of the location given.But when the location have 2 or more words it return error
ex: if $cityname have "Mexico City" then it returns error if it is only one word then it is return correctly
<?php
function get_latlng($cityname)
{
$Url='http://maps.googleapis.com/maps/api/geocode/json?address='.$cityname.'&sensor=false';
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$search_data = json_decode($output);
$new = array("lat"=>$search_data->results[0]->geometry->location->lat,"lng"=>$search_data->results[0]->geometry->location->lng);
return $new;
}
?>
This is the error produced
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object</p>
<p>Filename: admin/markers.php</p>
<p>Line Number: 19</p>
</div>
here admin/markers.php is my view page
this is the 19th line in my view page
$new = array("lat"=>$search_data->results[0]->geometry->location->lat,"lng"=>$search_data->results[0]->geometry->location->lng);