Find by latitude and longitude in mysql - 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 .

Related

How to get distance in kilometer when pass in 2 value (langtitude and longitude) in mysql?

I have a table in mysql call BRANCH
===============================
Branch_id latitude longitude
===============================
1 3.109421 101.622913
2 3.101121 101.644913
How can i select kilometer calculation from this table when I pass in my current location latitude / longitude?
Example:
If I pass in my current location = 3.122221 101.343913
============================================
Branch_id latitude longitude distance(km)
============================================
1 3.109421 101.622913 0.4
2 3.101121 101.644913 0.6
Edited (Solved):
SELECT p.title,p.subtitle,p.desc,p.image,p.promotion_id,p.merchant_id,p.date_from,p.date_to,m.merchant_name,p.view,mb.latitude,mb.longitude,
(6371 * acos (cos( radians(3.158704)) * cos( radians(mb.latitude))
* cos( radians(mb.longitude) - radians(101.713963)) + sin(radians(3.158704))
* sin( radians(mb.latitude)))) AS distance
FROM merchant_branch as mb
left join merchant m on mb.merchant_id = m.merchant_id
left join promotion p on p.merchant_id = m.merchant_id
where p.promotion_id is not null order by distance asc
From: https://developers.google.com/maps/solutions/store-locator/clothing-store-locator
Here's the SQL statement that finds the closest 20 locations within a
radius of 25 miles to the -33, 151 coordinate. It calculates the
distance based on the latitude/longitude of that row and the target
latitude/longitude, and then asks for only rows where the distance
value is less than 25, orders the whole query by distance, and limits
it to 20 results. To search by kilometers instead of miles, replace
3959 with 6371.
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

how to call MySql Stored procedure multiple time with multiple parameter.

CREATE PROCEDURE NearBy(IN VarLat VARCHAR(20), IN VarLng VARCHAR(20))
BEGIN
SELECT CustId,address, ( 6371 * acos( cos( radians(VarLat) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(VarLng) ) + sin( radians(VarLat) ) * sin( radians( Latitude ) ) ) ) AS distance FROM ordermaster HAVING distance < 0.02 ORDER BY distance LIMIT 0 , 10;
END;
call NearBy('21.1021258','79.0659928')
this my procedure to find nearby customer. parameter are lat and lng.
latitude and longitude are stored in database table.
i want result like no of order are coming form particular area of radius of 20 meter.

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.

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.

Using Match in mysql longitude and latitude search

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