


How to convert Baidu longitude and latitude to Tencent longitude and latitude using PHP
During the development process, we usually need to process some geographical location information, and the longitude and latitude formats of different platforms are different, which requires conversion. This article will introduce how to convert Baidu longitude and latitude into Tencent longitude and latitude, and use PHP code to implement it.
1. The difference between Baidu’s longitude and latitude and Tencent’s longitude and latitude
Longitude and latitude are symbols of the position on the earth’s surface. They are expressed in different ways in different positioning systems. There are currently three mainstream positioning systems: : WGS84, GCJ02 and BD09. Among them, WGS84 is the coordinate system used by the GPS positioning system, GCJ02 is the coordinate system of the geographic information system formulated by the China National Bureau of Surveying and Mapping, and is also the coordinate system that must be used by major domestic map software, while Baidu Maps uses BD09.
When using different positioning systems, the performance of longitude and latitude will also be different. For example, the Baidu longitude and latitude and Tencent longitude and latitude of a location are as follows:
Baidu longitude and latitude: 116.404,39.915
Tencent latitude and longitude: 116.397428,39.908697
2. Conversion method
Since Baidu and Tencent use different positioning systems, a certain algorithm is required to convert between longitude and latitude.
- Convert Baidu coordinate system to Earth coordinate system (WGS84)
First you need to convert Baidu coordinate system to Earth coordinate system (WGS84), which can be provided by Baidu API implementation, the code is as follows:
<?php function bd09_to_wgs84($bd_lon,$bd_lat){ $x_pi = 3.14159265358979324 * 3000.0 / 180.0; $x = $bd_lon - 0.0065; $y = $bd_lat - 0.006; $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi); $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi); $lng = $z * cos($theta); $lat = $z * sin($theta); return array('lng'=>$lng, 'lat'=>$lat); } ?>
- Convert the Earth coordinate system (WGS84) to the Mars coordinate system (GCJ02)
Use the function in the first step to get the earth After obtaining the result in the coordinate system (WGS84), it needs to be converted to the Mars coordinate system. This can be achieved by the following code:
<?php function wgs84_to_gcj02($lng, $lat) { $a = 6378245.0; $ee = 0.00669342162296594323; $dLat = $this->transformLat($lng - 105.0, $lat - 35.0); $dLng = $this->transformLng($lng - 105.0, $lat - 35.0); $radLat = $lat / 180.0 * pi(); $magic = sin($radLat); $magic = 1 - $ee * $magic * $magic; $sqrtMagic = sqrt($magic); $dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * pi()); $dLng = ($dLng * 180.0) / ($a / $sqrtMagic * cos($radLat) * pi()); $mgLat = $lat + $dLat; $mgLng = $lng + $dLng; return array('lng'=>$mgLng, 'lat'=>$mgLat); } function transformLat($lng, $lat) { $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng)); $ret += (20.0 * sin(6.0 * $lng * pi()) + 20.0 * sin(2.0 * $lng * pi())) * 2.0 / 3.0; $ret += (20.0 * sin($lat * pi()) + 40.0 * sin($lat / 3.0 * pi())) * 2.0 / 3.0; $ret += (160.0 * sin($lat / 12.0 * pi()) + 320 * sin($lat * pi() / 30.0)) * 2.0 / 3.0; return $ret; } function transformLng($lng, $lat) { $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng)); $ret += (20.0 * sin(6.0 * $lng * pi()) + 20.0 * sin(2.0 * $lng * pi())) * 2.0 / 3.0; $ret += (20.0 * sin($lng * pi()) + 40.0 * sin($lng / 3.0 * pi())) * 2.0 / 3.0; $ret += (150.0 * sin($lng / 12.0 * pi()) + 300.0 * sin($lng / 30.0 * pi())) * 2.0 / 3.0; return $ret; } ?>
- Convert the Mars coordinate system (GCJ02) to the Tencent coordinate system
The last step is to convert the Mars coordinate system (GCJ02) to Tencent coordinate system:
<?php function gcj02_to_tx($lng, $lat) { $x = $lng; $y = $lat; $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * pi()); $theta = atan2($y, $x) + 0.000003 * cos($x * pi()); $lng = $z * cos($theta) + 0.0065; $lat = $z * sin($theta) + 0.006; return array('lng'=>$lng, 'lat'=>$lat); } ?>
3. Complete code implementation
Combine the above three steps to get the complete code for converting Baidu longitude and latitude into Tencent longitude and latitude in PHP as follows:
<?php function bd09_to_wgs84($bd_lon,$bd_lat){ $x_pi = 3.14159265358979324 * 3000.0 / 180.0; $x = $bd_lon - 0.0065; $y = $bd_lat - 0.006; $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi); $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi); $lng = $z * cos($theta); $lat = $z * sin($theta); return array('lng'=>$lng, 'lat'=>$lat); } function wgs84_to_gcj02($lng, $lat) { $a = 6378245.0; $ee = 0.00669342162296594323; $dLat = $this->transformLat($lng - 105.0, $lat - 35.0); $dLng = $this->transformLng($lng - 105.0, $lat - 35.0); $radLat = $lat / 180.0 * pi(); $magic = sin($radLat); $magic = 1 - $ee * $magic * $magic; $sqrtMagic = sqrt($magic); $dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * pi()); $dLng = ($dLng * 180.0) / ($a / $sqrtMagic * cos($radLat) * pi()); $mgLat = $lat + $dLat; $mgLng = $lng + $dLng; return array('lng'=>$mgLng, 'lat'=>$mgLat); } function transformLat($lng, $lat) { $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng)); $ret += (20.0 * sin(6.0 * $lng * pi()) + 20.0 * sin(2.0 * $lng * pi())) * 2.0 / 3.0; $ret += (20.0 * sin($lat * pi()) + 40.0 * sin($lat / 3.0 * pi())) * 2.0 / 3.0; $ret += (160.0 * sin($lat / 12.0 * pi()) + 320 * sin($lat * pi() / 30.0)) * 2.0 / 3.0; return $ret; } function transformLng($lng, $lat) { $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng)); $ret += (20.0 * sin(6.0 * $lng * pi()) + 20.0 * sin(2.0 * $lng * pi())) * 2.0 / 3.0; $ret += (20.0 * sin($lng * pi()) + 40.0 * sin($lng / 3.0 * pi())) * 2.0 / 3.0; $ret += (150.0 * sin($lng / 12.0 * pi()) + 300.0 * sin($lng / 30.0 * pi())) * 2.0 / 3.0; return $ret; } function gcj02_to_tx($lng, $lat) { $x = $lng; $y = $lat; $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * pi()); $theta = atan2($y, $x) + 0.000003 * cos($x * pi()); $lng = $z * cos($theta) + 0.0065; $lat = $z * sin($theta) + 0.006; return array('lng'=>$lng, 'lat'=>$lat); } function bd09_to_tx($bd_lon, $bd_lat) { $point_wgs84 = $this->bd09_to_wgs84($bd_lon, $bd_lat); $point_gcj02 = $this->wgs84_to_gcj02($point_wgs84['lng'], $point_wgs84['lat']); $point_tx = $this->gcj02_to_tx($point_gcj02['lng'], $point_gcj02['lat']); return $point_tx; } ?>
Save the above code in a PHP file and you can use it.
4. Summary
Through the introduction of this article, we have learned the difference between Baidu longitude and latitude and Tencent longitude and latitude, and mastered the method of using PHP code to convert Baidu longitude and latitude into Tencent longitude and latitude. In actual projects, this conversion method can provide us with more convenient and accurate map information processing functions.
The above is the detailed content of How to convert Baidu longitude and latitude to Tencent longitude and latitude using PHP. For more information, please follow other related articles on the PHP Chinese website!

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



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.
