A simple example of mysql searching for data within N kilometers

A simple example of mysql searching for data within N kilometers

According to the coefficient of pi and the radius of the earth and the longitude and latitude of the search point, the distance between the search point and the search data table is within N kilometers.

1. Create a test table

CREATE TABLE `location` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(50) NOT NULL,
 `longitude` decimal(13,10) NOT NULL,
 `latitude` decimal(13,10) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `long_lat_index` (`longitude`,`latitude`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Insert test data

insert into location(name,longitude,latitude) values
('Guangzhou East Station',113.332264,23.156206),
('Lin Hexi',113.330611,23.147234),
('balance rack',113.328095,23.165376);

mysql> select * from `location`;
+----+--------------+----------------+---------------+
| id | name | longitude | latitude |
+----+--------------+----------------+---------------+
| 1 | Guangzhou East Railway Station | 113.3322640000 | 23.1562060000 |
| 2 | Linhexi | 113.3306110000 | 23.1472340000 |
| 3 | Balance frame | 113.3280950000 | 23.1653760000 |
+----+--------------+----------------+---------------+

3. Search for data within 1 km

Search point coordinates: Times Square 113.323568, 23.146436

6370.996 km is the radius of the Earth

Formula for calculating the coordinate distance between two points on a sphere

C = sin(MLatA)sin(MLatB)cos(MLonA-MLonB) + cos(MLatA)cos(MLatB) 
Distance = RArccos(C)*Pi180

According to the calculation formula, the query statement is as follows:

select * from `location` where (
acos(
sin(([#latitude#]*3.1415)/180) * sin((latitude*3.1415)/180) + 
cos(([#latitude#]*3.1415)/180) * cos((latitude*3.1415)/180) * cos(([#longitude#]*3.1415)/180 - (longitude*3.1415)/180)
)*6370.996
)<=1;

Execute the query:

mysql> select * from `location` where (
  -> acos(
  -> sin((23.146436*3.1415)/180) * sin((latitude*3.1415)/180) + 
  -> cos((23.146436*3.1415)/180) * cos((latitude*3.1415)/180) * cos((113.323568*3.1415)/180 - (longitude*3.1415)/180)
  -> )*6370.996
  -> )<=1;
+----+-----------+----------------+---------------+
| id | name | longitude | latitude |
+----+-----------+----------------+---------------+
| 2 | Linhexi | 113.3306110000 | 23.1472340000 |
+----+-----------+----------------+---------------+

The above simple example of MySQL searching for data within N kilometers is all I want to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

<<:  Three ways to configure Nginx virtual hosts (based on domain names)

>>:  vue uses Ele.me UI to imitate the filtering function of teambition

Recommend

Six weird and useful things about JavaScript

Table of contents 1. Deconstruction Tips 2. Digit...

Three Discussions on Iframe Adaptive Height Code

When building a B/S system interface, you often en...

js development plug-in to achieve tab effect

This article example shares the specific code of ...

How to install Nginx in CentOS

Official documentation: https://nginx.org/en/linu...

Docker installation and configuration steps for Redis image

Table of contents Preface environment Install Cre...

Tutorial on installing jdk1.8 on ubuntu14.04

1. Download jdk download address我下載的是jdk-8u221-li...

Vue3.0 adaptive operation of computers with different resolutions

First we need to install some dependencies npm i ...

Summary of ways to implement single sign-on in Vue

The project has been suspended recently, and the ...

Markup Language - Anchor

Previous: Markup Language - Phrase Elements Origin...

HTML basic summary recommendation (title)

HTML: Title Heading is defined by tags such as &l...

Several methods to clear floating (recommended)

1. Add an empty element of the same type, and the...

Use jQuery to fix the invalid page anchor point problem under iframe

The application scenario is: the iframe page has n...

Detailed tutorial on deploying Apollo custom environment with docker-compose

Table of contents What is the Apollo Configuratio...

Detailed steps for implementing timeout status monitoring in Apache FlinkCEP

CEP - Complex Event Processing. The payment has n...

Monitor the size change of a DOM element through iframe

A common problem encountered during the developme...