mysql函数-依据经纬度坐标计算距离
mysql函数-根据经纬度坐标计算距离 在实际的Web开发中,越来越多的碰到一些特定领域的专业算法,比如本文提到的根据两点经纬度计算距离的方法。 通常的两种计算: 1。要算的距离是椭球面的距离 设第一点A的经纬度为(LonA, LatA),第二点B的经纬度为(LonB, LatB
mysql函数-根据经纬度坐标计算距离在实际的Web开发中,越来越多的碰到一些特定领域的专业算法,比如本文提到的根据两点经纬度计算距离的方法。
通常的两种计算:
1。要算的距离是椭球面的距离
设第一点A的经纬度为(LonA, LatA),第二点B的经纬度为(LonB, LatB),按照0度经线的基准,东经取经度的正值(Longitude),西经取经度负值(-Longitude),北纬取90-纬度值(90- Latitude),南纬取90+纬度值(90+Latitude),则经过上述处理过后的两点被计为(MLonA, MLatA)和(MLonB, MLatB)。那么根据三角推导,可以得到计算两点距离的如下公式:
C = sin(MLatA)*sin(MLatB)*cos(MLonA-MLonB) + cos(MLatA)*cos(MLatB)
Distance = R*Arccos(C)*Pi/180
这里,R和Distance单位是相同,如果是采用6371.004千米作为半径,那么Distance就是千米为单位,如果要使用其他单位,比如mile,还需要做单位换算,1千米=0.621371192mile
如果仅对经度作正负的处理,而不对纬度作90-Latitude(假设都是北半球,南半球只有澳洲具有应用意义)的处理,那么公式将是:
C = sin(LatA)*sin(LatB) + cos(LatA)*cos(LatB)*cos(MLonA-MLonB)
Distance = R*Arccos(C)*Pi/180
2。google地图提供的方法(PHP版本)
class Distance{ const EARTH_RADIUS = 6378.137; // earth radius (const) kilometer /** * * Example: * $precision = 49; * ini_set("precision", $precision); * echo pi(); //will output 3.141592653589793115997963468544185161590576171875 * * 1 kilometer = 0.621371192 mile * * @param double d * @return double data */ private function rad( $d ) { return $d * M_PI / 180.0; } /** * * Example : get_distance(44.2112,-88.4175,34.5082,-82.6498) * Get Distance between two point : longitude,latitude(lat1,lng1 => lat2,lng2 ) * * @param double d * @return double data * */ public function get_distance( $lat1, $lng1, $lat2, $lng2, $base = 1000 ) { $radLat1 = floatval( $this->rad($lat1) ); $radLat2 = floatval( $this->rad($lat2) ); $a = $radLat1 - $radLat2; $b = $this->rad($lng1) - $this->rad($lng2); $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))); $s = $s * self::EARTH_RADIUS; $s = round($s * 10000) / 10000; return $s; } }
第一种方法计算繁琐,我们通常采用的还是第二种方法。但是,无论是第一种还是第二种如果直接用编程语言来转换的话,会非常耗时,因为,我们通常不得不将全部结果查出,然后再逐个计算过滤以得到结果集,如果需要排序,那将更加耗时。
一个优化的方法就是采用SQL函数,具体的Mysql Function请参见附件,注意其中的括号,默认单位是公里,如果要转换成英里,请参考本文中PHP Code注释部分自行修改。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.
