


Mysql stored procedure - query the qualified users within the radius and the sorted distance through Baidu coordinates
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 ;
Call:
CALL DIS(_lat,_lng,_ras,@_data);
_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; }
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.

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



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.

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

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

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

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.

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

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

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.
