Using Match in mysql longitude and latitude search - mysql

I'm trying to put together a simple location search where people can enter a keyword and a location and the nearest results are displayed.
I can get the mysql statement for the free text search and the radius search working individually but not together...
My attempt is below...
SELECT
place_id,
(
3959 * acos (
cos ( radians(52.586973) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(-2.128820000000019) )
+ sin ( radians(52.586973) )
* sin( radians( lat ) )
)
) AS distance
FROM places
HAVING distance < 30,
MATCH (title,details) AGAINST ('Cafe') AS score
FROM places
WHERE MATCH (title, details)
AGAINST ('Cafe')
ORDER BY distance

Related

SQL query to find closest distance between coordinates is running very slowly

I have the following query which does indeed work. It takes in a hard set Latitude and Longitude and finds the closest set of coordinates, along with it's postal code.
What I'm having trouble with is the speed of the query. It's currently taking 3.8 seconds to run and I have 9000 separate coordinates to check.
Any advice on how to speed this up would be greatly appreciated.
SELECT pcds, ROUND(MIN(distance), 4) AS distance FROM
(SELECT `postcode`.`pcds`,(
6371 * acos (
cos ( radians('51.4932392') )
* cos( radians( `postcode`.`lat` ) )
* cos( radians( `postcode`.`lng` ) - radians('-0.0846429') )
+ sin ( radians('51.4932392') )
* sin( radians( `postcode`.`lat` ) )
)
) AS distance
FROM postcode
ORDER BY distance
LIMIT 1
) AS First

MySQL - Find points within radius from database

I have a table which has a POINT column containing the latitude and longitude of various locations.
I then also have a users location from geo-location in the browser.
What I need to be able to do is find all records from the table where the POINT value in the is within a 10 km radius (or X km radius), ordered by distance with the closest first.
My table has a SPATIAL index on the POINT column.
I'm currently working on a project where I'm calculating distances between multiple locations. I'm using the following query for selecting object_id's which are within a given radius.
SELECT id,
( 6371 *
ACOS(
COS( RADIANS( db_latitude ) ) *
COS( RADIANS( $user_latitude ) ) *
COS( RADIANS( $user_longitude ) -
RADIANS( db_longitude ) ) +
SIN( RADIANS( db_latitude ) ) *
SIN( RADIANS( $user_latitude) )
)
)
AS distance FROM the_table HAVING distance <= $the_radius ORDER BY distance ASC"
I can't explain the ACOS formula itself because I got it from research.
db_latitude = database latitude field
db_longitude = database longitude field
$user_latitude = browser latitude coördinate
$user_longitude = browser longitude coördinate
$the_radius = the radius that you want to search in
This is in kilometers.
The query below actually worked for me :
$query = "SELECT *,
( 6371 *
acos(
cos( radians( ".$user_lat." ) ) *
cos( radians( lat ) ) *
cos( radians( lng ) -
radians( ".$user_lng." ) ) +
sin( radians( ".$user_lat." ) ) *
sin( radians( lat ) ) ) )
AS distance FROM parkings
HAVING distance <= ".$radius." ORDER BY distance ASC";
$stmt = $conn->execute($query);
$rows = $stmt->fetchAll('assoc');
where:
$user_lat and $user_lng is browser's lat and lng,
$radius = 10,
table name is parkings
May be this help for you,
https://ru.scribd.com/presentation/2569355/Geo-Distance-Search-with-MySQL
For Django I use this
dist = 20 #дистанция 20 км
mylon = 51.5289156201 # долгота центра
mylat = 46.0209384922 # широта
lon1 = mylon-dist/abs(math.cos(math.radians(mylat))*111.0) # 1 градус широты = 111 км
lon2 = mylon+dist/abs(math.cos(math.radians(mylat))*111.0)
lat1 = mylat-(dist/111.0)
lat2 = mylat+(dist/111.0)
profiles = UserProfile.objects.filter(lat__range=(lat1, lat2)).filter(lon__range=(lon1, lon2))
It search all users in squar 20km.

MySQL sub query with two columns - how to hide the second column?

I'm having a difficult time wrapping my head around how to do this, even with all the searching and reading I've done on this!
I have a query I am using to try to pull all users from a database that have a zip code that falls within a certain distance of a given decimal coordinate. Here's the query that I am running:
select distinct watch_list.username, enabled
from watch_list, registered_users
where watch_list.username = registered_users.username AND watch_list.watchzip = (
SELECT zip,
( 3959 * acos( cos( radians('29.7632800') ) *
cos( radians( lat ) ) *
cos( radians( lng ) -
radians('-95.3632700') ) +
sin( radians('29.7632800') ) *
sin( radians( lat ) ) ) )
AS distance from zip
HAVING distance <= '10');
There error I get back is expected, as my sub query is returning two columns:
MySQL said: Documentation
#1241 - Operand should contain 1 column(s)
How can I do this and filter on the distance without the sub query returning both columns?
P.S. Just for completion and information sake, the "zip" table contains a list of all zip codes in the U.S. along with their decimal coordinates.
Just move the operation out of the column list:
select distinct watch_list.username, enabled
from watch_list, registered_users
where watch_list.username = registered_users.username
AND watch_list.watchzip = (
SELECT zip
from zip
WHERE ( 3959 * acos( cos( radians('29.7632800') ) *
cos( radians( lat ) ) *
cos( radians( lng ) -
radians('-95.3632700') ) +
sin( radians('29.7632800') ) *
sin( radians( lat ) ) ) ) <= '10');
Edit: As P.Salmon mentions, you probably also want to change AND watch_list.watchzip = to AND watch_list.watchzip IN.

Which unit is `distance` returning in this SQL query

I'm messing with some latitude/longitude related code in the Google Maps API which provides me with the following SQL statement
SELECT id, (
3959 * acos( cos( radians(37) ) *
cos( radians( lat ) ) *
cos( radians( lng ) - radians(-122) ) +
sin( radians(37) ) * sin( radians( lat ) ) ) )
AS distance
FROM marker
HAVING distance < 25
ORDER BY distance LIMIT 0 , 20;
Link to Google Maps example here: https://developers.google.com/maps/articles/phpsqlsearch_v3
However the distance returned is over 7,000, while I know that all of my entries in the database are within 50 miles of each other. I've verified this using http://www.movable-type.co.uk/scripts/latlong.html which also goes over the Haversine formula.
So I'm curious as to what I'm doing wrong with the provided query, which returns:
(IMAGE)
http://gyazo.com/ece247747616c5a412edd40c82c4b0ce -- (Failed to upload image, format not accepted???).
All points are compared from
long: 39.410870
lat: -107.102180
Here's the full query:
SELECT id,`user_long`,`user_lat`,
( 3959 * acos( cos( radians(39.410870) ) * cos( radians( `user_lat` ) ) * cos( radians( `user_long` ) - radians(-107.102180) ) + sin( radians(39.410870) ) * sin( radians( `user_lat` ) ) ) ) AS distance
FROM `accounts`
ORDER BY distance LIMIT 0 , 20
As you can see in the results, it's really strange, because even when compared to itself, the distance is > 7000.
ID: 1
LONG: 39.410870
LAT: -107.102180
DIST: 7923.067131806453
Units is in miles.
It looks like you've got latitude and longitude reversed/swapped.
Valid range for latitude is only -90 to +90, it can't be -107 (degrees).
If you mean to specify a location in Colorado west of Denver, halfway to Grand Junction, swap the values for latitude and longitude.
The "great circle calculation" looks correct, it's going to return miles, since you are multiplying by 3959. (You'd need to replace that constant with a different one to get distance units other than miles.)
It looks like you've got the fixed latitude and longitude correctly place in the expression... I suspect it's the values in user_lat and user_lng that are swapped/reversed.

Find by latitude and longitude in mysql

i want to search near by location with given latitude and longitude in mysql
latitude is : 26.902
longitude is : 75.793
and distance is : 30
Query is :
SELECT
id, (
3959 * acos (
cos ( radians(26.902) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(75.793) )
+ sin ( radians(26.902) )
* sin( radians( latitude ) )
)
) AS distance
FROM business
HAVING distance < 30
ORDER BY distance
LIMIT 0 , 20;
Result:
i am getting a record with distance is 3.58,
record lat/long are 26.89 / 75.74
but when i check online on other site i got distance 5.759 miles .
Some code available here as a Stored Function: http://mysql.rjweb.org/doc.php/latlng .