Home  >  Article  >  Database  >  MySQL creates a geographical coordinate table to implement location positioning function

MySQL creates a geographical coordinate table to implement location positioning function

PHPz
PHPzOriginal
2023-07-01 22:17:151529browse

MySQL creates a geographical coordinate table to implement location positioning function

Title: MySQL creates a geographical coordinate table to implement location positioning function

Abstract: With the rapid development of the mobile Internet, the location positioning function has become an integral part of many applications. This article will introduce how to use MySQL to create a geographical coordinate table to implement the location positioning function, and provide code examples.

1. Introduction

In many application scenarios, we need to perform data query or provide corresponding services based on the user's location information. MySQL provides support for geographical coordinates. After creating a geographical coordinate table, we can easily perform operations such as location positioning and distance calculation.

2. Create a geographical coordinate table

To create a geographical coordinate table, we need to use the spatial extension function of MySQL. First, make sure your version of MySQL supports spatial expansion, then execute the following code:

-- 创建地理坐标表
CREATE TABLE coordinates (
    id INT AUTO_INCREMENT PRIMARY KEY,
    location POINT,
    address VARCHAR(255)
);

This code creates a table named coordinates, which contains three fields: id is the auto-increment primary key, location is the geographical coordinates, and address is the address information.

3. Insert geographical coordinate data

When inserting data into the geographical coordinate table, we can perform coordinate conversion and calculation by using the related functions provided by MySQL. The following is an example:

-- 插入地理坐标数据
INSERT INTO coordinates (location, address)
VALUES (POINT(30.2888, -97.7054), 'Austin, TX'),
       (POINT(37.7749, -122.4194), 'San Francisco, CA'),
       (POINT(40.7128, -74.0060), 'New York, NY');

This code inserts three geographical coordinate data into the coordinates table.

4. Example of location positioning function

By creating a geographical coordinate table, we can easily implement the location positioning function. Here is a sample code:

-- 查询距离最近的地点
SELECT address,
       ST_Distance_Sphere(location, POINT(32.7767, -96.7970)) AS distance
FROM coordinates
ORDER BY distance
LIMIT 1;

This code will query for the closest location to the given coordinates (32.7767, -96.7970) and return the address and distance of that location.

5. Summary

Through MySQL's geographical coordinate table, we can easily perform operations such as location positioning and distance calculation. This article introduces how to create a geographic coordinate table and provides relevant code examples. I hope it will be helpful for you to understand and use the geographical coordinates function!

The above is the detailed content of MySQL creates a geographical coordinate table to implement location positioning function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn