I'm currently working on a web app that will rate neighbourhoods on a number of different criteria, one of which being statistics from the local Garda station (police station in Irish). I'm passing a latitude and longitude value from the client side into a controller in Spring and I want to run the following SQL query so I can find the nearest police station and return the relevant statistics.
Set @lat = x; // Local Variable for a latitude value
Set @lng = y; // Local Variable for a longitude value
SELECT * FROM garda_station
ORDER BY ((garda_station_latitude - @lat)*
(garda_station_latitude - @lat))+
((garda_station_longitude - @lng)*
(garda_station_longitude - @lng))
ASC LIMIT 1;
The garda_station table I'm trying to access is structured as follows
+-------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------+--------------+------+-----+---------+----------------+
| garda_station_id | int(11) | NO | PRI | NULL | auto_increment |
| garda_station_latitude | double | NO | | NULL | |
| garda_station_longitude | double | NO | | NULL | |
| garda_station_name | varchar(255) | YES | | NULL | |
+-------------------------+--------------+------+-----+---------+----------------+
I'm passing the latitude & longitude values from the client side via an AJAX request to a Controller class in my Spring project.
@RequestMapping(value = "/results", method = RequestMethod.POST)
public @ResponseBody
String Submit(@RequestParam("latitude") String latitude,@RequestParam("longitude") String longitude) {
// I'm printing them for now to ensure they have been sent from the client side
System.out.println(latitude + "" + longitude);
return "/";
}
This is my first big attempt at a spring project so if there were any other suggestions as to how I could conduct this SQL query I'd love to heat your suggestions.