How to find nearest location using latitude and longitude from sql database?

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

I want to find a nearest location from following database table

I have fetched this all data from google maps. Here i have to find nearest location from a place. Suppose i am at place Surkhet its latitude is 28.6 and longitude is 81.6, how can i find nearest place from the place Surkhet.

回答1:

Finding locations nearby with MySQL

Here's the SQL statement that will find the closest 20 locations that are within a radius of 25 miles to the 37, -122 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.

Table Structure :

id,name,address,lat,lng 

NOTE - Here latitude = 37 & longitude = -122. So you just pass your own.

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) *  cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) *  sin( radians( lat ) ) ) ) AS distance FROM your_table_name HAVING distance 

You can find details here.



回答2:

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

This is the Best Query



回答3:

To find the nearby location , you can use the Geocoder Class.Since you have the Geopoints(latitude and longitude), Reverse geocoding can be used. Reverse Geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. Check out this for more information.



回答4:

The SQL have a problem. In table like:

`ubicacion` (   `id_ubicacion` INT(10) NOT NULL AUTO_INCREMENT ,   `latitud` DOUBLE NOT NULL ,   `longitud` DOUBLE NOT NULL ,   PRIMARY KEY (`id_ubicacion`) ) 

The id_ubicacion change when use:

SELECT  `id_ubicacion` , ( 3959 * ACOS( COS( RADIANS( 9.053933 ) ) * COS( RADIANS( latitud ) ) * COS( RADIANS( longitud ) - RADIANS( - 79.421215 ) ) + SIN( RADIANS( 9.053933 ) ) * SIN( RADIANS( latitud ) ) ) ) AS distance FROM ubicacion HAVING distance 


回答5:

sesach : ( 3959 * acos( cos( radians('.$_REQUEST['latitude'].') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$_REQUEST['longitude'].') ) + sin( radians('.$_REQUEST['latitude'].') ) * sin( radians( latitude ) ) ) ) 


回答6:

When the method should perform it is nicer to first filter on latitude and longitude, and then calculate the squared distance approximative. For Nordic countries, it will be about 0.3 percent off within 1000 km's.

So instead of calculatinG the distance as:

dist_Sphere = r_earth * acos ( sin (lat1) * sin (lat2) + cos(lat1)*cos(lat2)*cos(lon 2 - lon 1) 

one can calculate the approximate value (assume that lat = lat 1 is close to lat 2) as

const cLat2 = cos lat ^ 2 const r_earth2 = r_earth ^ 2 dist_App ^2 = r_earth2 * ((lat 2 - lat 1) ^2 + clat2 *(lon 2 - lon 1) ^2) 

Order by Dist_App 2, and then simply take the square root off the result.



回答7:

SELECT latitude, longitude, SQRT(     POW(69.1 * (latitude - [startlat]), 2) +     POW(69.1 * ([startlng] - longitude) * COS(latitude / 57.3), 2)) AS distance FROM TableName HAVING distance 

where [starlat] and [startlng] is the position where to start measuring the distance and 25 is the distance in kms.

It is advised to make stored procedure to use the query because it would be checking a lots of rows to find the result.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!