I am trying to develop a small site that extracts results from a database with a given radius and puts them on a map. I use google api maps to get latin and long criteria from there, pass them to my php script, which queries the database and returns the result set as a JSON object.
Im having a little problem sending both lat and long to php using json.
My main problem is that my sql for searching the database seems wrong, as it just retrieves the first 10 results in my database. I want it to return the first 10 results to the radius from the search point.
Here is my ajax code
function showCarPark(location) { var lat = location.lat(); var lng = location.lng(); //alert("Lat: " +lat.toFixed(6)); //alert("Lng: " +lng.toFixed(6)); document.getElementById('carParkResults').innerHTML = ""; var criterion = document.getElementById("address").value; var count = 0; $.ajax({ url: 'process.php', type: 'GET', data: "lat=" + lat + "&lng=" + lng, dataType: 'json', success: function(data) { jQuery.each(data, function() { $('<p>').text("Car Park: " + data[count].name).appendTo('#carParkResults'); placeCarParks(data[count].postcode,data[count].name, data[count].street, data[count].type); count++; }); }, error: function(e) { //called when there is an error console.log(e.message); alert("error" + e.message); } });
And here is my PHP script
$rad = 20; $lat = $_GET['lat']; $lng = 1.4681464; //put a temporary number in as it wont pass the lng in the JSON $sql="SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(lat)) * cos( radians(long) - radians('".$lng."')) + sin(radians('".$lat."')) * sin(radians(lat)))) AS distance FROM carpark HAVING distance < 15 ORDER BY distance LIMIT 0 , 10"; $result = mysql_query($sql); while($r = mysql_fetch_assoc($result)) $rows[] = $r; echo json_encode($rows);
The columns in my table are called lat and long. Any help is appreciated.
source share