How to use MySQL's geometry type to handle longitude and latitude distance problems

How to use MySQL's geometry type to handle longitude and latitude distance problems

Create a table

CREATE TABLE `map` (
 `id` int(11) NOT NULL,
 `address` varchar(255) NOT NULL DEFAULT '',
 `location` geometry NOT NULL,
 PRIMARY KEY (`id`),
 SPATIAL KEY `idx_location` (`location`)
)

insert

INSERT INTO map (id, address, location) VALUES (1, 'somewhere', ST_GeomFromText('POINT(121.366961 31.190049)'));

Note that you must use the ST_GeomFromText function, and POINT() contains: longitude + space + latitude

Query

1. Check the latitude and longitude

SELECT address, ST_AsText(location) AS location FROM map;

2. Calculate the distance between two points

SELECT ST_Distance_Sphere(POINT(121.590347, 31.388094),location) AS distant FROM map;

The calculated result is in meters.

Note that the latitude and longitude in POINT() are now separated by commas.

3. Search for locations within 1000m and sort them from far to near

Copy the code as follows:
SELECT id, address, ST_Distance_Sphere(POINT(121.590347, 31.388094),location) AS distant FROM map WHERE ST_Distance_Sphere(POINT(121.590347, 31.388094),location) < 1000 ORDER BY distant;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Mybatis learning road mysql batch add data method
  • Detailed explanation of how to implement secondary cache with MySQL and Redis
  • In-depth explanation of MySQL common index and unique index
  • How to view mysql binlog (binary log)
  • Docker creates MySQL explanation
  • PHP date() format MySQL insert datetime method
  • How to solve SQL injection problem with pymysql
  • Steps to export the fields and related attributes of MySQL tables
  • Solution to the problem of MySQL thread in Opening tables
  • MySQL limit performance analysis and optimization

<<:  Solve the cross-domain problem of get and post requests of vue $http

>>:  Detailed explanation of how Tomcat implements asynchronous Servlet

Recommend

Web page custom selection box Select

Everyone may be familiar with the select drop-dow...

Vue-CLI multi-page directory packaging steps record

Page directory structure Note that you need to mo...

How to run JavaScript in Jupyter Notebook

Later, I also added how to use Jupyter Notebook i...

Detailed tutorial on installing JDK1.8 on Linux

1. Cleaning before installation rpm -qa | grep jd...

Learning about UDP in Linux

Table of contents 1. Introduction to UDP and Linu...

JavaScript implements simple calculator function

This article example shares the specific code of ...

How to expand the disk size of a virtual machine

After Vmvare sets the disk size of the virtual ma...

Detailed explanation of the cache implementation principle of Vue computed

Table of contents Initialize computed Dependency ...

Introduction and examples of hidden fields in HTML

Basic syntax: <input type="hidden" na...

A brief discussion on the role of Vue3 defineComponent

Table of contents defineComponent overload functi...

Detailed explanation of important cascading concepts in CSS

Recently, I encountered a problem in the process ...

Some functions of using tcpdump to capture packets in the Linux command line

tcpdump is a flexible and powerful packet capture...

A brief introduction to web2.0 products and functions

<br />What is web2.0? Web2.0 includes those ...