Home Backend Development PHP Tutorial Mysql stored procedure - query the qualified users within the radius and the sorted distance through Baidu coordinates

Mysql stored procedure - query the qualified users within the radius and the sorted distance through Baidu coordinates

Aug 08, 2016 am 09:31 AM
declare range varchar

I have made a similar application before. I took a look at it today and felt that it was not very reasonable, so I reconsidered and wrote a query stored procedure. The

table is not described, the

process is as follows:

-- ----------------------------
-- Procedure structure for DIS
-- ----------------------------
DROP PROCEDURE IF EXISTS `DIS`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DIS`(IN `_lat` varchar(11),IN `_lng` varchar(11),IN `_ras` int,INOUT `_data` text)
BEGIN
	DECLARE _sql text ;
	DECLARE EARTH_RADIUS VARCHAR(10) ;
	DECLARE _range VARCHAR(16);
	DECLARE lngR VARCHAR(16);
	DECLARE maxLat VARCHAR(16);
	DECLARE minLat VARCHAR(16);
	DECLARE maxLng VARCHAR(16);
	DECLARE minLng VARCHAR(16);



	SET EARTH_RADIUS = 6378.137;
	SET _range = 180 / pi() * _ras / EARTH_RADIUS;
	SET lngR = _range / cos(_lat * pi() / 180);
	
	SET maxLat = _lat + _range;
	SET minLat = _lat - _range;
	SET maxLng = _lng + lngR ;
	SET minLng = _lng - lngR ;

	SET @_sql = CONCAT('SELECT 
				*, 
				ceil(
					2 * asin(
						sqrt(
							pow(sin((((',_lat,' * PI() / 180.0) - (',_lat,' * PI() / 180.0))) / 2),2) 
							+ cos(',_lat,' * PI() / 180.0) * cos(lat * PI() / 180.0) * pow(sin(((',_lng,' * PI() / 180.0) - (lng * PI() / 180.0)) / 2),2)
						)
					) * ',EARTH_RADIUS,' * 1000
				) AS di 
			FROM dis 
			WHERE lat BETWEEN ',minLat,' AND ',maxLat,' AND lng BETWEEN ',minLng,' AND ',maxLng,' 
			ORDER BY di
			LIMIT 0,10');
	
	PREPARE stmt FROM @_sql;
	EXECUTE stmt;

END
;;
DELIMITER ;
Copy after login

Call:

CALL DIS(_lat,_lng,_ras,@_data);
Copy after login
Parameter description:

_lat, longitude;

_lng, latitude;

_ras, radius (unit is km);

@_data, accept and return

In the return value, di is the distance that has been sorted, from near to far, the unit is m.

If you do not use stored procedures, you can split it into a PHP program:

define("EARTH_RADIUS",6378.137);
/**
 * 获取距离四个坐标
 * @param $lon
 * @param $lat
 * @param int $distance 默认1KM的距离
 * @return array
*/
function getCoor($lng,$lat,$distance = 1){
	$range = 180 / pi() * $distance / EARTH_RADIUS;
	$lngR = $range / cos($lat * pi() / 180);
	$data = array();
	$data["maxLat"] = $lat + $range;
	$data["minLat"] = $lat - $range;
	$data["maxLng"] = $lng + $lngR ;//最大经度
	$data["minLng"] = $lng - $lngR ;//最小经度
	return $data;
}
Copy after login

First get a square coordinate area, and then splice it into a sql statement through the sql in @_sql above, and then execute it.

Remarks, the result obtained here is a square rather than a circular radius. You can do a secondary filtering at a later stage, such as array_filter(), which will not be described further here.

The above introduces the Mysql stored procedure - using Baidu coordinates to query qualified users within the radius and the distance after sorting, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the usages of oracle declare What are the usages of oracle declare Sep 15, 2023 pm 01:12 PM

Oracle declare usage includes variable declaration, constant declaration, cursor declaration and subroutine declaration. Detailed introduction: 1. Variable declaration, in PL/SQL block, you can use the DECLARE statement to declare variables; 2. Constants are unchangeable values ​​declared in PL/SQL block; 3. Cursor declaration, used in PL/SQL The query result set is processed in the block; 4. Subroutine declaration. A subroutine is a reusable code block defined in a PL/SQL block.

PHP returns all the values ​​in the array to form an array PHP returns all the values ​​in the array to form an array Mar 21, 2024 am 09:06 AM

This article will explain in detail how PHP returns all the values ​​of an array to form an array. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Using the array_values() function The array_values() function returns an array of all the values ​​in an array. It does not preserve the keys of the original array. $array=["foo"=>"bar","baz"=>"qux"];$values=array_values($array);//$values ​​will be ["bar","qux"]Using a loop can Use a loop to manually get all the values ​​of the array and add them to a new

How to use Range function in Java How to use Range function in Java Apr 19, 2023 pm 11:49 PM

Preface In Java, the Range method is available in both IntStream and LongStream classes. In IntStream class, it helps to return the sequential value of IntStream within function parameter scope. In the method, startInclusive(inclusive) and endExclusive(exclusive) are the two parameters used along with the increment step size, as mentioned before, the start value will be included and the end value will be excluded. In the case of LongStream, the only difference is the addition of the LongStream value. Range Syntax Let us see the syntax of range method in Java. IntStream range

Explain in simple terms: Thoroughly understand the working principle of Go language range Explain in simple terms: Thoroughly understand the working principle of Go language range Mar 12, 2024 pm 02:18 PM

Go language is a concise and powerful programming language with unique design and features in many aspects. One of the most impressive features is the range keyword, which is used to iterate over data structures such as arrays, slices, maps, and channels. The flexibility and convenience of range make it easy to traverse complex data structures, but many people are confused about how it works. This article will explain how range works in a simple and in-depth way, and use specific code examples to help readers better understand. First, let's look at a simple example

How to use declare in Oracle How to use declare in Oracle Sep 19, 2023 am 09:44 AM

The DECLARE statement is a keyword used in Oracle to declare variables, constants and cursors. Provides a convenient way to define and initialize data objects for use in PL/SQL blocks. By using the DECLARE statement, PL/SQL code can be made more modular and readable.

How to implement date comparison, sorting and other operations with the varchar type in mysql How to implement date comparison, sorting and other operations with the varchar type in mysql May 31, 2023 pm 01:49 PM

During the use of MySQL, dates are generally stored in datetime, timestamp and other formats. However, sometimes due to special needs or historical reasons, the date is stored in varchar format. So how should we process the date data in varchar format? ? Use function: STR_TO_DATE(str,format) The STR_TO_DATE(str,format) function is the inverse function of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains date and time components, or if the word

Enormous Cadillac Escalade IQ offers 460 miles of range at up to $170,000 price Enormous Cadillac Escalade IQ offers 460 miles of range at up to $170,000 price Sep 30, 2024 am 06:15 AM

GM's electric take on the legendary Escalade just got its configurator up, and the price ranges from$130,000 to about $170,000 after incentives. This is more than nearly any other luxury electric vehicle, pickup, or SUV out there, including Tesla's C

What is the difference between char and varchar in mysql What is the difference between char and varchar in mysql Sep 04, 2023 pm 02:16 PM

The differences between char and varchar in mysql are: 1. CHAR is fixed length, while VARCHAR is variable length; 2. CHAR storage and retrieval efficiency is high, while VARCHAR storage and retrieval efficiency is not high; 3. CHAR takes up storage space, VARCHAR can save storage space.

See all articles