GPS positioning, according to latitude and longitude to query the latitude and longitude of the location-sql method to achieve

Source: Internet
Author: User
Tags acos cos mssql sin

According to the current coordinate point is also the latitude and longitude, to find the database near 5 kilometers or 10 kilometers near the realization of all the information, after looking for information, the original is I sophomore learn, is to seek the chord length, mathematics forget, did not think mathematics is so useful, mathematics ah is really use too big.

Use of what cosine, angle, radian all forget, but by looking for information, or achieved, the error is relatively small, I implemented in the MSSQL database, specifically as follows:

MSSQL function:

ALTER FUNCTION [getdistance]( @GPSLng DECIMAL( A,6),@GPSLat DECIMAL( A,6),@Lng  DECIMAL( A,6),@Lat DECIMAL( A,6))RETURNS DECIMAL( A,4) asBEGIN   DECLARE @result DECIMAL( A,4)   SELECT @result=6371.004*ACOS(SIN(@GPSLat/ the*PI())*SIN(@Lat/ the*PI())+COS(@GPSLat/ the*PI())*COS(@Lat/ the*PI())*COS((@GPSLng-@Lng)/ the*PI()))   RETURN @resultEND

SQL query statement:

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

Find records within 5 km of the database

Note:6371.004Earth radius6371.004*ACOS(SIN(@GPSLat/ the*PI())*SIN(@Lat/ the*PI())+COS(@GPSLat/ the*PI())*COS(@Lat/ the*PI())*COS((@GPSLng-@Lng)/ the*PI()) A detailed explanation of the calculation formula for calculating the spherical arc length

Reprint: http://blog.csdn.net/ecdyf1989/article/details/8077943/

GPS positioning, according to latitude and longitude to query the latitude and longitude of the location-sql method to achieve

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.