In the database, according to the latitude and longitude to find all the adjacent longitude and latitude point __ Database

Source: Internet
Author: User
Tags acos cos mssql sin

According to the current location of the coordinate point is also latitude and longitude, find the database near 5 km or 10 km near the implementation of all information, after looking for information, it is my sophomore school, is to find the string length, math forgot, did not think math is so useful, math ah really use too much.

Use what cosine, angle, radian all forget, but by looking for data, or realized, the error is relatively small, I implemented in the MSSQL database, specific as follows:


MSSQL functions:

ALTER FUNCTION [Getdistance] (
@GPSLng DECIMAL (12,6),
@GPSLat DECIMAL (12,6),
@Lng DECIMAL (12,6),
@Lat DECIMAL (12,6)
)
RETURNS DECIMAL (12,4)
As
BEGIN
DECLARE @result DECIMAL (12,4)
SELECT @result =6371.004*acos SIN (@GPSLat/180*pi ()) *sin (@Lat/180*pi ()) +cos (@GPSLat/180*pi ()) *cos (@Lat/180*pi ()) * COS ((@GPSLng-@Lng)/180*pi ())
Return @result

End



SQL query statement:

SELECT *,getdistance (longitude at some point, latitude of a certain point, longitude in database, latitude of database) as dis from table name dis<5

Find records within 5 km of a database


Note:

6371.004 Earth Radius

6371.004*acos (SIN (@GPSLat/180*pi ()) *sin (@Lat/180*pi ()) +cos (@GPSLat/180*pi ()) *cos (@Lat/180*pi ()) *cos (@ gpslng-@Lng)/180*pi ())--detailed explanation of the calculation formula of spherical arc length




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.