using this query from google: link
Im trying to create a location search, returning all towns within a set distance.
I want people to be able to input the town in a form, my code find the lat + long of the town name, and then query my database to return all towns within set distance.
to start, i find the lat + long of the inputted location:
$location = 'farnborough';
function getlatlang($location) {
$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='. urlencode($location) .'&sensor=false');
$output= json_decode($geocode);
return $output->results[0]->geometry->location;
}
$objlocation = getlatlang($location);
$latitude = $objlocation->lat;
$longitude = $objlocation->lng;
this works fine, i can echo the result and the values are correct.
then using the query in the above link, i try:
$query = "SELECT town, ( 3959 * acos( cos( radians($latitude) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( lat ) ) ) ) AS distance FROM uk_postcode_05 HAVING distance < 25 ORDER BY distance ASC LIMIT 0 , 5";
$result = mysqli_query($mysqli, $query);
if(!$result){echo('no result returned - query wrong');
}
but the queries doesnt work and always fires the error. what am i doing wrong here?
(my table has columns town, postcode, latitude, longitude)
I’m making the assumption here, that you have changed the column names from lat, lng to latitude, longitude in your table definition.
And under the lack of the actual error message, this is the best guess:
this should fix the issue, since in your query
latandlngwhere undefined.